Skip to content

Commit

Permalink
Unarchiving logic update
Browse files Browse the repository at this point in the history
  • Loading branch information
uerceg committed Mar 19, 2020
1 parent 4f07ab7 commit a191250
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions Adjust/ADJUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -286,25 +286,35 @@ + (NSDictionary *)buildJsonDict:(NSData *)jsonData
+ (id)readObject:(NSString *)fileName
objectName:(NSString *)objectName
class:(Class)classToRead {
// Try to read from Application Support directory first.
NSString *documentsFilePath = [ADJUtil getFilePathInDocumentsDir:fileName];
NSString *appSupportFilePath = [ADJUtil getFilePathInAppSupportDir:fileName];

// Try to read from Application Support directory first.
@try {
id appSupportObject = [NSKeyedUnarchiver unarchiveObjectWithFile:appSupportFilePath];
if ([appSupportObject isKindOfClass:classToRead]) {
// Successfully read object from Application Support folder, return it.
if ([appSupportObject isKindOfClass:[NSArray class]]) {
[[ADJAdjustFactory logger] debug:@"Package handler read %d packages", [appSupportObject count]];
} else {
[[ADJAdjustFactory logger] debug:@"Read %@: %@", objectName, appSupportObject];
id appSupportObject;
if (@available(iOS 11.0, *)) {
NSData *data = [NSData dataWithContentsOfFile:appSupportFilePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[unarchiver setRequiresSecureCoding:NO];
appSupportObject = [unarchiver decodeObjectOfClass:classToRead forKey:NSKeyedArchiveRootObjectKey];
} else {
appSupportObject = [NSKeyedUnarchiver unarchiveObjectWithFile:appSupportFilePath];
}

if (appSupportObject != nil) {
if ([appSupportObject isKindOfClass:classToRead]) {
// Successfully read object from Application Support folder, return it.
if ([appSupportObject isKindOfClass:[NSArray class]]) {
[[ADJAdjustFactory logger] debug:@"Package handler read %d packages", [appSupportObject count]];
} else {
[[ADJAdjustFactory logger] debug:@"Read %@: %@", objectName, appSupportObject];
}

// Just in case check if old file exists in Documents folder and if yes, remove it.
[ADJUtil deleteFileInPath:documentsFilePath];

return appSupportObject;
}
// Just in case check if old file exists in Documents folder and if yes, remove it.
[ADJUtil deleteFileInPath:documentsFilePath];
return appSupportObject;
} else if (appSupportObject == nil) {
// [[ADJAdjustFactory logger] verbose:@"%@ file not found", appSupportFilePath];
[[ADJAdjustFactory logger] verbose:@"%@ file not found in \"Application Support/Adjust\" folder", fileName];
} else {
// [[ADJAdjustFactory logger] error:@"Failed to read %@ file", appSupportFilePath];
[[ADJAdjustFactory logger] error:@"Failed to read %@ file from \"Application Support/Adjust\" folder", fileName];
Expand All @@ -317,21 +327,29 @@ + (id)readObject:(NSString *)fileName
// If in here, for some reason, reading of file from Application Support folder failed.
// Let's check the Documents folder.
@try {
id documentsObject = [NSKeyedUnarchiver unarchiveObjectWithFile:documentsFilePath];
id documentsObject;
if (@available(iOS 11.0, *)) {
NSData *data = [NSData dataWithContentsOfFile:documentsFilePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[unarchiver setRequiresSecureCoding:NO];
documentsObject = [unarchiver decodeObjectOfClass:classToRead forKey:NSKeyedArchiveRootObjectKey];
} else {
documentsObject = [NSKeyedUnarchiver unarchiveObjectWithFile:documentsFilePath];
}

if (documentsObject != nil) {
// Successfully read object from Documents folder.
if ([documentsObject isKindOfClass:[NSArray class]]) {
[[ADJAdjustFactory logger] debug:@"Package handler read %d packages", [documentsObject count]];
} else {
[[ADJAdjustFactory logger] debug:@"Read %@: %@", objectName, documentsObject];
}

// Do the file migration.
[[ADJAdjustFactory logger] verbose:@"Migrating %@ file from Documents to \"Application Support/Adjust\" folder", fileName];
[ADJUtil migrateFileFromPath:documentsFilePath toPath:appSupportFilePath];

return documentsObject;
} else if (documentsObject == nil) {
// [[ADJAdjustFactory logger] verbose:@"%@ file not found", documentsFilePath];
[[ADJAdjustFactory logger] verbose:@"%@ file not found in Documents folder", fileName];
} else {
// [[ADJAdjustFactory logger] error:@"Failed to read %@ file", documentsFilePath];
[[ADJAdjustFactory logger] error:@"Failed to read %@ file from Documents folder", fileName];
Expand All @@ -340,6 +358,7 @@ + (id)readObject:(NSString *)fileName
// [[ADJAdjustFactory logger] error:@"Failed to read %@ file (%@)", documentsFilePath, ex];
[[ADJAdjustFactory logger] error:@"Failed to read %@ file from Documents folder (%@)", fileName, ex];
}

return nil;
}

Expand Down

0 comments on commit a191250

Please sign in to comment.