Friday, 14 August 2015

Data persistence and app updates


hi,

i'm developing app requires property list first time, , i'm little confused file system , conventions of using it. especially, when future app versions developed , plist needs updating.

when app opens, plist should installed persists lifetime of app version (i.e. until updated or uninstalled). i'd know tips , tricks experienced developers may have dealing situations this.

suppose logical development strategy goes this:

when writing sandbox directory
// search directory , find path (append name of file, extension, on path)
// if not found, create
// log error

found code online mentioned taking app versions consideration, makes lot of sense if intend update app in future. not of steps clear me , i'm hoping can me understand steps i'm unclear with.

// search directory , instantiate nsuserdefaults ready key/value retrieve of app version in next step
code:
nsfilemanager *deffm = [nsfilemanager defaultmanager];  	nsstring *docsdir = [nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask, yes) objectatindex:0];  	nsuserdefaults *userdefaults = [nsuserdefaults standarduserdefaults];
// statically declare current app version , retrieve older app version nsuserdefaults, compare. if appropriate, execute block.
// block sets app version, assigning new key/value in nsuserdefaults.
// block continues to: find appdirectory , docdirectory? find confusing. should data saved?
code:
//..stuff done once when installing new version....  	static nsstring *appversionkey = @"myappversion";  	int lastversion = [userdefaults integerforkey: appversionkey];  	if( lastversion != thisversion )	//..do once after install..  	{  		[userdefaults setinteger: thisversion forkey: appversionkey];  		nsstring *appdir = [[nsbundle mainbundle] resourcepath];  		nsstring *src = [appdir stringbyappendingpathcomponent: @"xcodemade.plist"];  		nsstring *dest = [docsdir stringbyappendingpathcomponent: @"xcodemade.plist"];  		[deffm removeitematpath: dest error: null];  //..remove old copy  		[deffm copyitematpath: src topath: dest error: null];  	}  //..end of stuff done once when installing new version.
would write code differently, or cover pretty well?
 

yes, write differently. not think in "files containing preferences". think in terms of values , keys make preference. let nsuserpreferences persisting. not overwrite existing settings user has chosen.

migration strategy should add new keys nsuserdefaults dictionary. check version stored in nsuserpreferences: if version indicates setting known access key. if version indicates setting unknown add setting using defaults.

ever do, keep users settings. user tend upset if app deletes settings during update.

- olaf
 


Forums iPhone, iPad, and iPod Touch iOS Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment