views:

27

answers:

2

Hi, I create an app, with data store in plist. Now what are the wAys I cAn update the data? Maybe if I create a webpage and retrieve my dAta from it instead of storing in plist? Or is there a way I can update my data when I plug into iTunes? Or any other suggestion? WhAt I want to achieve is a way of updating my data once user have download the app.

Thks

A: 

Plists are not easily modifiable since you have to read them back into memory, make the changes and write them back to the file.

For writing the plists look at NSDictionary's or NSArray's writeToFile:atomically: method. Remember your plist is basically an XML made up from NSArrays, NSDictionaries, NSStrings and NSData, and you'll always want to write out the root node.

Alternatively look for other ways of storing your data: user defaults or sqlite databases. Storing data 'in the cloud' makes sense only in some cases, like if you want to share it between different devices or want to update it remotely. It's best to avoid it if possible.

MihaiD
It's best to do it well and not avoid it. E.g. update the data when ever there is a connection and a update and than cache it locally. Store the version of the data or date of update in a plist with a `NSMutableDictionary`.
Thomas
Hi i would to update it remotely. Maybe something like, i can send an alert telling user an update is avaliable etc. Or like YouTube app, how does it store/retrieve all the data?
Stefan
Hi Thomas how can I achieve the method u suggest?
Stefan
A: 
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dir = [path objectAtIndex:0];
NSString *filePath = [dir stringByAppendingPathComponent:@"my.plist"];

NSMutableDictionary *localDictionary;
NSUrl *remoteUrl = [NSURL URLWithString:@"http://server.com/my.plist"];
NSMutableDictionary *remoteDictionary = [NSMutableDictionary dictionaryWithContentsOfURL:remoteUrl];
if(remoteDictionary != nil) {
     [remoteDictionary writeToFile:filePath atomically:YES];
     localDictionary = remoteDictionary;
}
else {
     localDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
     if(localDictionary == nil) localDictionary = [NSMutableDictionary dictionary];
}

This will try to obtain a copy of my.plist from http://server.com/, on success it will write it to the disk and point localDictionary to it, on error it will try to read from disk (e.g. downloaded earlier when there was a connection), if there is no file on disk it will create a empty NSMutableDictionary. Either way localDictionary will point to a NSMutableDictionary in the end.

This is very simple and will request the file every time the code is run. You could e.g. use NSFileModificationDate (obtained by NSFileManger attributesOfItemAtPath:error:) and NSDate to determine if a update is necessary. Or for big files versioning would make sense as well, having a small file containing the version of the bigger file - getting the small file from the server (e.g. with NSString), checking whether the cached version file contains a lower version number, if so get the big file. It always depends on how often the content is refreshed.

See full documentation: NSDictionary, NSString, NSFileManager, NSDate

Thomas
Hi,my.plist is the webpage file or? Sorry I am still new to this. Is the server a free to use service? Thanks for the help
Stefan
my.plist is just a plist file (see http://en.wikipedia.org/wiki/Property_list) which is very convenient to use for smallish amounts of data since you can serialize and write it to file with only one command and reverse the process in one command as well. A server is something you have to get your self, normally you would need a domain as well (maybe 2 subdomains which can be routed to different server in case one goes down). Instead of a web server (http) you could also use a FTP server. If you need help with that you should ask somewhere else since hosting isn't programming.
Thomas