Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 5, 2012, 01:59 AM   #1
nashyo
macrumors 6502
 
Join Date: Oct 2010
Location: Bristol
Data persistence and app updates

Hi,

I'm developing an app that requires a property list for the first time, and I'm a little confused about the file system and the conventions of using it. Especially, when future app versions are developed and the plist likely needs updating.

When my app opens, the plist should be installed so that it persists for the lifetime of the app version (i.e. until it is updated or uninstalled). I'd like to know any tips and tricks experienced developers may have for dealing with situations like this.

I suppose the logical development strategy goes like this:

When writing to sandbox directory
// search for directory and find path (append name of file, with extension, on path)
// if not found, create
// log any error

I found some code online that mentioned taking app versions into consideration, which makes a lot of sense if you intend to update your app in the future. But not all of the steps are clear to me and I'm hoping someone can help me understand the steps i'm unclear with.

// search for directory and instantiate NSUserDefaults ready for 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 and retrieve older app version from NSUserDefaults, then compare. if appropriate, execute block.
// block sets app version, by assigning new key/value in NSUserDefaults.
// block continues to: find appDirectory and docDirectory? this i find confusing. Where should this data be saved?
Code:
//..Stuff that is done only once when installing a new version....
	static NSString *AppVersionKey = @"MyAppVersion";
	int lastVersion = [userDefaults integerForKey: AppVersionKey];
	if( lastVersion != thisVersion )	//..do this only 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 only once when installing a new version.
Would anyone write this code differently, or does this cover it pretty well?
__________________
Xcode 4.5.2, SDK 6.0, OSX Mountain Lion, ARC
nashyo is offline   0 Reply With Quote
Old Feb 5, 2012, 06:39 AM   #2
forum user
macrumors regular
 
Join Date: Aug 2008
Quote:
Originally Posted by nashyo View Post
Would anyone write this code differently, or does this cover it pretty well?
Yes, write it differently. Do not think in "files containing preferences". Think in terms of the values and keys that make up the preference. Let NSUserPreferences do the persisting. Do not overwrite existing settings that the user has chosen.

Your migration strategy should be to add the new keys to the NSUserDefaults dictionary. Check the version stored in the NSUserPreferences: If version indicates that a certain setting is known then access the key. If version indicates a certain setting is unknown then add certain setting using defaults.

What ever you do, keep the users settings. User tend to get upset if an app deletes their settings during an update.

- Olaf
__________________
Fuel Log Evo: The Cost Manager App for any Vehicle
forum user is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
file, nsuserdefaults, plist, version, versions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPad: Any way to turn off app store indication for app updates on jailbroken ipad mini? Boulware28 Jailbreaks and iOS Hacks 7 Oct 18, 2013 06:33 PM
Cellular data automatic updates leftynaut iOS 7 6 Sep 11, 2013 12:35 PM
iPad Mini: App updates not showing in updates Idbvideo iPad 3 Mar 6, 2013 07:46 AM
Apple Updates OS X Server, Adds Caching to Speed Distribution of Mac App Store Updates MacRumors Mac Blog Discussion 26 Dec 12, 2012 01:26 AM
VMWare Fusion non-persistence ztb Mac Basics and Help 0 Aug 20, 2012 08:17 PM

Forum Jump

All times are GMT -5. The time now is 10:50 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC