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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
App store chart stagnant and not updating danderton iPhone and iPod touch Apps 5 Jan 3, 2012 08:32 AM
Apps get rearranged & folders get deleted after app updates... mnijaw iPod touch 6 Jul 4, 2011 11:11 AM
How to keep data without the app? Kuzminator iPhone Tips, Help and Troubleshooting 0 Jun 3, 2011 11:19 PM
iOS 4.3 New app update design and bugs. GollumBoy iPhone 10 Jan 15, 2011 08:18 AM
App Updates not appearing in App Store on iPad Billboard iPad 3 Nov 22, 2010 10:39 PM


All times are GMT -5. The time now is 01:19 PM.

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

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