user1419181
user1419181

Reputation: 1

Persisting data in property list

I am using plist to store and retrieve my favorite list. Everything works fine till I restart my Iphone. The favorite list disappears. My question is can I use plist to persist data? Please can someone assist if possible ? Thanks

Upvotes: 0

Views: 203

Answers (4)

Bruno Ferreira
Bruno Ferreira

Reputation: 952

I'm not experienced on ios development but I sugest you to use NSUserDefaults.
I'm using it on a Mac OS X application and it does the job.

Upvotes: 0

Abhishek
Abhishek

Reputation: 2253

Yes you can use to p-list to persist data and copy your p-list in document directory ... by using following code:

NSError *error;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"your_plist_name.plist"];

may this will help you...

Upvotes: 0

Felix Lamouroux
Felix Lamouroux

Reputation: 7484

How do you store and reload your plist? In general it should not be a problem. Simply save the plist to the app's documents directory when going to the background (or whenever something changes) and reload on start.

Simply use the following method (for NSArray or NSDictionary):

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

You should not use NSUserDefaults as others suggested, as these are meant for user defaults only and not for user data. Saving to the document's directory makes sure that the data is backed up correctly and works well even for larger sets of data.

NSUserDefaults is meant for things that you could potentially display in a settings view.

Upvotes: 2

EsbenB
EsbenB

Reputation: 3406

As bruno suggest why not store the user pref data in the NSUserDefault.

You can do something like this:

To save:

self.userDefaults = [NSUserDefaults standardUserDefaults];
[self.userDefaults setObject:myObject forKey:@"myKey"];
[self synchronize];

To load:

self.userDefaults = [NSUserDefaults standardUserDefaults]; 
NSObject *myObject = [self.userDefaults objectForKey:@"myKey"];

Upvotes: 0

Related Questions