ios - Singleton archiving not unarchiving usable instance -


i've run trouble trying archive singleton project. technically applies 2 singletons archive under single wrapper array purposes of question, i'll involve "notebook" singleton.

my problem recalling archive doesn't reproduce instance correctly, appears "null" app otherwise performs.

my idea archive singleton once app enters background, here's method in appdelegate:

- (void)applicationdidenterbackground:(uiapplication *)application { // use method release shared resources, save user data, invalidate timers, , store enough application state information restore application current state in case terminated later. // if application supports background execution, method called instead of applicationwillterminate: when user quits.  settings *sharedsettings = [settings sharedsettings]; notebook *sharednotebook = [notebook sharednotebook]; nsarray *array_wrapperforsave = @[sharedsettings, sharednotebook];  nsarray *archivedirectory = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *archivepathforarray = [archivedirectory objectatindex:0]; nsstring *directoryforarray = [archivepathforarray stringbyappendingstring:@"userdatabundle.archive"]; archivepath = directoryforarray;  bool success = [nskeyedarchiver archiverootobject:array_wrapperforsave tofile:archivepath];  nslog(@"data archive status: %d", success); 

that nslog returns 1 (success)

here notebook.m's initializer(s), important offending line last method call:

#pragma mark - initializers  - (instancetype)init {     self = [super init];     if (self) {         array_highlights = [[nsmutablearray alloc] init];         array_sections = [[nsmutablearray alloc] init];         sharedsettings = [settings sharedsettings];         indexoflastloadedsection = 0;          section *rootsection = [[section alloc] initwithtitle:@"main tab"];         [self savesection:rootsection];  //        self = [self accessarchivedinstance];     // causes huge issues.     }     return self; }  - (instancetype)initwithcoder:(nscoder *)adecoder {     self = [super init];     if (self) {         array_sections = [adecoder decodeobjectforkey:@"array_sections"];         array_highlights = [adecoder decodeobjectforkey:@"array_highlights"];         indexoflastloadedsection = [adecoder decodeintegerforkey:@"indexoflastloadedsection"];     }     return self; } 

here method grabs file path wrapper array , uncases proper object instance @ index:

- (notebook *)accessarchivedinstance {     nsarray *archivedirectory = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);     nsstring *archivepathforarray = [archivedirectory objectatindex:0];     nsstring *directoryforarray = [archivepathforarray stringbyappendingstring:@"userdatabundle.archive"];       nsarray *array_archivedsingletons = [nskeyedunarchiver unarchiveobjectwithfile:directoryforarray];     notebook *returninstance = [array_archivedsingletons objectatindex:1];      nslog(@"notebook instance awaking archive");      return returninstance; } 

singleton method:

+ (id)sharednotebook {     static notebook *sharednotebook = nil;     static dispatch_once_t oncetoken;     dispatch_once(&oncetoken, ^{         sharednotebook = [[self alloc] init];     });      return sharednotebook; } 

i can of course provide info if needed. in advance!

the main issue never use accessarchiveinstance , when tried in wrong place.

modify sharednotebook be:

+ (id)sharednotebook {     static notebook *sharednotebook = nil;     static dispatch_once_t oncetoken;     dispatch_once(&oncetoken, ^{         nsarray *archivedirectory = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);         nsstring *archivepathforarray = [archivedirectory objectatindex:0];         nsstring *directoryforarray = [archivepathforarray stringbyappendingpathcomponent:@"userdatabundle.archive"];         if ([[nsfilemanager defaultmanager] fileexistsatpath:directoryforarray]) {             nsarray *array_archivedsingletons = [nskeyedunarchiver unarchiveobjectwithfile:directoryforarray];             sharednotebook = [array_archivedsingletons objectatindex:1];         } else {             sharednotebook = [[self alloc] init];         }     });      return sharednotebook; } 

and remove accessarchiveinstance method.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -