Skip to content

Commit 4e6b8e0

Browse files
authored
Merge pull request #486 from BranchMetrics/pref-location-INTENG-2882
Fix for INTENG-2882
2 parents 22b7196 + 53a0154 commit 4e6b8e0

File tree

4 files changed

+163
-30
lines changed

4 files changed

+163
-30
lines changed

Branch-SDK/Branch-SDK/BNCPreferenceHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
@property (assign, nonatomic) NSInteger installRequestDelay;
4040

4141
+ (BNCPreferenceHelper *)preferenceHelper;
42+
+ (NSURL*) URLForBranchDirectory;
4243

4344
- (NSString *)getAPIBaseURL;
4445
- (NSString *)getAPIURL:(NSString *)endpoint;

Branch-SDK/Branch-SDK/BNCPreferenceHelper.m

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,25 @@ - (void)writeObjectToDefaults:(NSString *)key value:(NSObject *)value {
610610
}
611611

612612
- (void)persistPrefsToDisk {
613-
NSDictionary *persistenceDict = [self.persistenceDict copy];
614-
NSBlockOperation *newPersistOp = [NSBlockOperation blockOperationWithBlock:^{
615-
if (![NSKeyedArchiver archiveRootObject:persistenceDict toFile:[self prefsFile]]) {
616-
[self logWarning:@"Failed to persist preferences to disk"];
617-
}
618-
}];
613+
@synchronized (self) {
614+
NSDictionary *persistenceDict = [self.persistenceDict copy];
615+
NSBlockOperation *newPersistOp = [NSBlockOperation blockOperationWithBlock:^ {
616+
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:persistenceDict];
617+
if (!data) {
618+
[self logWarning:@"Can't create preferences archive."];
619+
return;
620+
}
621+
NSError *error = nil;
622+
[data writeToURL:self.class.URLForPrefsFile
623+
options:NSDataWritingAtomic error:&error];
624+
if (error) {
625+
[self logWarning:
626+
[NSString stringWithFormat:
627+
@"Failed to persist preferences to disk: %@.", error]];
628+
}
629+
}];
619630
[self.persistPrefsQueue addOperation:newPersistOp];
631+
}
620632
}
621633

622634
#pragma mark - Reading From Persistence
@@ -625,20 +637,23 @@ - (NSMutableDictionary *)persistenceDict {
625637
if (!_persistenceDict) {
626638
NSDictionary *persistenceDict = nil;
627639
@try {
628-
persistenceDict = [NSKeyedUnarchiver unarchiveObjectWithFile:[self prefsFile]];
640+
NSError *error = nil;
641+
NSData *data = [NSData dataWithContentsOfURL:self.class.URLForPrefsFile
642+
options:0 error:&error];
643+
if (error || !data)
644+
NSLog(@"Error opening prefs file: %@.", error);
645+
else
646+
persistenceDict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
629647
}
630648
@catch (NSException *exception) {
631-
[self logWarning:@"Failed to load preferences from disk"];
649+
[self logWarning:@"Failed to load preferences from disk."];
632650
}
633651

634-
if (persistenceDict) {
652+
if ([persistenceDict isKindOfClass:[NSDictionary class]])
635653
_persistenceDict = [persistenceDict mutableCopy];
636-
}
637-
else {
654+
else
638655
_persistenceDict = [[NSMutableDictionary alloc] init];
639-
}
640656
}
641-
642657
return _persistenceDict;
643658
}
644659

@@ -672,8 +687,69 @@ - (NSInteger)readIntegerFromDefaults:(NSString *)key {
672687
return NSNotFound;
673688
}
674689

675-
- (NSString *)prefsFile {
676-
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:BRANCH_PREFS_FILE];
690+
+ (NSString *)prefsFile_deprecated {
691+
NSString * path =
692+
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
693+
firstObject]
694+
stringByAppendingPathComponent:BRANCH_PREFS_FILE];
695+
return path;
696+
}
697+
698+
+ (NSURL*) URLForBranchDirectory {
699+
NSError *error = nil;
700+
NSURL *URL =
701+
[[NSFileManager defaultManager]
702+
URLForDirectory:NSApplicationSupportDirectory
703+
inDomain:NSUserDomainMask
704+
appropriateForURL:nil
705+
create:YES
706+
error:&error];
707+
if (error) {
708+
NSLog(@"Error creating URLForPrefsDirectory: %@.", error);
709+
return nil;
710+
}
711+
URL = [URL URLByAppendingPathComponent:@"io.branch"];
712+
[[NSFileManager defaultManager]
713+
createDirectoryAtURL:URL
714+
withIntermediateDirectories:YES
715+
attributes:nil
716+
error:&error];
717+
if (error) {
718+
NSLog(@"Error creating URLForPrefsDirectory: %@.", error);
719+
return nil;
720+
}
721+
return URL;
722+
}
723+
724+
+ (NSURL*) URLForPrefsFile {
725+
NSURL *URL = [self URLForBranchDirectory];
726+
URL = [URL URLByAppendingPathComponent:BRANCH_PREFS_FILE];
727+
return URL;
728+
}
729+
730+
+ (void) moveOldPrefsFile {
731+
NSURL *oldURL = [NSURL fileURLWithPath:self.prefsFile_deprecated];
732+
NSURL *newURL = [self URLForBranchDirectory];
733+
734+
NSError *error = nil;
735+
[[NSFileManager defaultManager]
736+
moveItemAtURL:oldURL
737+
toURL:newURL
738+
error:&error];
739+
740+
if (error && error.code != NSFileNoSuchFileError) {
741+
if (error.code == NSFileWriteFileExistsError) {
742+
[[NSFileManager defaultManager]
743+
removeItemAtURL:oldURL
744+
error:&error];
745+
} else {
746+
NSLog(@"Error moving prefs file: %@.", error);
747+
}
748+
}
749+
}
750+
751+
+ (void) initialize {
752+
[self moveOldPrefsFile];
677753
}
678754

679755
@end

Branch-SDK/Branch-SDK/BNCServerRequestQueue.m

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,12 @@ - (void)persistEventually {
187187

188188
- (void)persistImmediately {
189189
[self.writeTimer invalidate];
190-
191190
[self persistToDisk];
192191
}
193192

194193
- (void)persistToDisk {
195194
NSArray *requestsToPersist = [self.queue copy];
196-
dispatch_async(self.asyncQueue, ^{
195+
dispatch_async(self.asyncQueue, ^ {
197196
@try {
198197
NSMutableArray *encodedRequests = [[NSMutableArray alloc] init];
199198
for (BNCServerRequest *req in requestsToPersist) {
@@ -205,13 +204,26 @@ - (void)persistToDisk {
205204
NSData *encodedReq = [NSKeyedArchiver archivedDataWithRootObject:req];
206205
[encodedRequests addObject:encodedReq];
207206
}
208-
209-
if (![NSKeyedArchiver archiveRootObject:encodedRequests toFile:[self queueFile]]) {
210-
[[BNCPreferenceHelper preferenceHelper] logWarning:@"Failed to persist queue to disk"];
207+
208+
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:encodedRequests];
209+
if (!data) {
210+
[[BNCPreferenceHelper preferenceHelper]
211+
logWarning:@"Cannot create archive data."];
212+
return;
213+
}
214+
NSError *error = nil;
215+
[data writeToURL:self.class.URLForQueueFile
216+
options:NSDataWritingAtomic error:&error];
217+
if (error) {
218+
[[BNCPreferenceHelper preferenceHelper] logWarning:
219+
[NSString stringWithFormat:@"Failed to persist queue to disk: %@", error]];
211220
}
212221
}
213222
@catch (NSException *exception) {
214-
NSString *warningMessage = [NSString stringWithFormat:@"An exception occurred while attempting to save the queue. Exception information:\n\n%@", [self exceptionString:exception]];
223+
NSString *warningMessage =
224+
[NSString stringWithFormat:
225+
@"An exception occurred while attempting to save the queue. Exception information:\n\n%@",
226+
[self exceptionString:exception]];
215227
[[BNCPreferenceHelper preferenceHelper] logWarning:warningMessage];
216228
}
217229
});
@@ -223,10 +235,19 @@ - (void)retrieve {
223235

224236
// Capture exception while loading the queue file
225237
@try {
226-
encodedRequests = [NSKeyedUnarchiver unarchiveObjectWithFile:[self queueFile]];
238+
NSError *error = nil;
239+
NSData *data = [NSData dataWithContentsOfURL:self.class.URLForQueueFile options:0 error:&error];
240+
if (error || !data)
241+
[[BNCPreferenceHelper preferenceHelper] logWarning:
242+
[NSString stringWithFormat:@"Error loading network queue: %@.", error]];
243+
else
244+
encodedRequests = [NSKeyedUnarchiver unarchiveObjectWithData:data];
227245
}
228246
@catch (NSException *exception) {
229-
NSString *warningMessage = [NSString stringWithFormat:@"An exception occurred while attempting to load the queue file, proceeding without requests. Exception information:\n\n%@", [self exceptionString:exception]];
247+
NSString *warningMessage =
248+
[NSString stringWithFormat:
249+
@"An exception occurred while attempting to load the queue file, proceeding without requests. Exception information:\n\n%@",
250+
[self exceptionString:exception]];
230251
[[BNCPreferenceHelper preferenceHelper] logWarning:warningMessage];
231252
self.queue = queue;
232253
return;
@@ -265,8 +286,42 @@ - (NSString *)exceptionString:(NSException *)exception {
265286
return [NSString stringWithFormat:@"Name: %@\nReason: %@\nStack:\n\t%@\n\n", exception.name, exception.reason, [exception.callStackSymbols componentsJoinedByString:@"\n\t"]];
266287
}
267288

268-
- (NSString *)queueFile {
269-
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:BRANCH_QUEUE_FILE];
289+
+ (NSString *)queueFile_deprecated {
290+
NSString *path =
291+
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
292+
firstObject]
293+
stringByAppendingPathComponent:BRANCH_QUEUE_FILE];
294+
return path;
295+
}
296+
297+
+ (NSURL*) URLForQueueFile {
298+
NSURL *URL = [BNCPreferenceHelper URLForBranchDirectory];
299+
URL = [URL URLByAppendingPathComponent:BRANCH_QUEUE_FILE];
300+
return URL;
301+
}
302+
303+
+ (void) moveOldQueueFile {
304+
NSURL *oldURL = [NSURL fileURLWithPath:self.queueFile_deprecated];
305+
NSURL *newURL = [self URLForQueueFile];
306+
NSError *error = nil;
307+
[[NSFileManager defaultManager]
308+
moveItemAtURL:oldURL
309+
toURL:newURL
310+
error:&error];
311+
312+
if (error && error.code != NSFileNoSuchFileError) {
313+
if (error.code == NSFileWriteFileExistsError) {
314+
[[NSFileManager defaultManager]
315+
removeItemAtURL:oldURL
316+
error:&error];
317+
} else {
318+
NSLog(@"Error moving queue file: %@.", error);
319+
}
320+
}
321+
}
322+
323+
+ (void) initialize {
324+
[self moveOldQueueFile];
270325
}
271326

272327
#pragma mark - Singleton method
@@ -275,12 +330,15 @@ + (id)getInstance {
275330
static BNCServerRequestQueue *sharedQueue = nil;
276331
static dispatch_once_t onceToken;
277332

278-
dispatch_once(&onceToken, ^{
333+
dispatch_once(&onceToken, ^ {
279334
sharedQueue = [[BNCServerRequestQueue alloc] init];
280335
[sharedQueue retrieve];
281-
[[BNCPreferenceHelper preferenceHelper] log:FILE_NAME line:LINE_NUM message:@"Retrieved from Persist: %@", sharedQueue];
336+
[[BNCPreferenceHelper preferenceHelper]
337+
log:FILE_NAME
338+
line:LINE_NUM
339+
message:@"Retrieved from Persist: %@", sharedQueue];
282340
});
283-
341+
284342
return sharedQueue;
285343
}
286344

Branch-TestBed/Branch-TestBed.xcodeproj/project.pbxproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,6 @@
11091109
};
11101110
7E6B3B581AA42D0E005F45BF /* Debug */ = {
11111111
isa = XCBuildConfiguration;
1112-
baseConfigurationReference = 31573E92BB6FA3902CA53858 /* Pods-Branch-SDK-Tests.debug.xcconfig */;
11131112
buildSettings = {
11141113
CLANG_WARN_UNREACHABLE_CODE = YES;
11151114
ENABLE_STRICT_OBJC_MSGSEND = YES;
@@ -1132,7 +1131,6 @@
11321131
};
11331132
7E6B3B591AA42D0E005F45BF /* Release */ = {
11341133
isa = XCBuildConfiguration;
1135-
baseConfigurationReference = 1715515941BF9D80F30AE170 /* Pods-Branch-SDK-Tests.release.xcconfig */;
11361134
buildSettings = {
11371135
CLANG_WARN_UNREACHABLE_CODE = YES;
11381136
ENABLE_STRICT_OBJC_MSGSEND = YES;

0 commit comments

Comments
 (0)