state saving - iOS Autosaved object -


in app have singleton object, should save state through app launches. need save somehow.

i see 2 options: 1) save on app termination (plus, maybe, going background); 2) save each time property changed.

first option looks bad, because app can killed, example, because of bug, memory limits or device power-off (low battery). expect state won't saved.

second option needs either manual notifications each change, or kvo + observing of each property.

seems wrong here. maybe, can give me advice or there well-known pattern (i've tried google, found nothing particular).

update:

yes, there nsuserdefaults, improve usability (smth. more key-values) write wrapper-methords, end same problem (lines of manual coding).

update2:

coredata bad choice me: 1 object store + inserting there needs more lines of code.

update3:

it's not question "how save". it's "how call saving automatically (or less of coding)". in nsuserdefault way need manually implement each property wrapper. in nscoding - call save or post notification (to catch & save 1 place) each property also.

the simplest way save user states in ios through nsuserdefaults.

here example keeps track of changed made singleton:

@interface mysingleton : nsobject { }  + (mysingleton *)sharedsingleton;  @property (copy) nsstring *username;//the variable track @end  @implementation mysingleton @synthesize username; + (mysingleton *)sharedsingleton {   static mysingleton *sharedsingleton;    @synchronized(self)   {     if (!sharedsingleton)       sharedsingleton = [[mysingleton alloc] init];      return sharedsingleton;   }      }  - (void)setusername:(nsstring*)username {    //update variable    self.username = username;    //saves new value nsuserdefaults    [[nsuserdefaults standarduserdefaults] setobject:username forkey:@"username"];     //forces instantaneous synchronization, not needed, updated after periodic interval anyways    [[nsuserdefaults standarduserdefaults] synchronize]; } @end 

optionally can implement nscoding protocol , save whole class nsuserdefaults, take @ this question.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -