@@ -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 URLForPrefsFile ];
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
0 commit comments