Persist data between views
I am developing an app in which the user enter a search word and receives results from a web service (I control the web service). The results are displayed on a uitable view and then the user can select a row and nav开发者_JAVA百科igate to three - four level of inner details (I am using the uinavigation control).
I am wondering what is the best way to persist the data between the views.
I am currently using the application delegate to store an array of objects which I can access from everywhere in the app. It works fine but I read that it is not a good practice for the job (I am concerned about memory issues) . I tried using Core Data framework for the job but than I realized that I would have to read my web service results, and loop them one by one in order to insert them to database. I also will have to delete old data because the data I am saving is only actual for the current search. I read about p-lists and saving data to file system but could not find a real good solution for my job... Any help will be appreciated!!! Thanks!!!
You can use JSON or PLISt for communication, although binary plist is - according to apple - much faster on the phone side.
Creating a dictionary from the binary Plist is relatively simple:
NSPropertyListFormat format;
NSDictionary *myDictionary = [NSPropertyListSerialization
propertyListFromData:data mutabilityOption:NSPropertyListMutableContainers
format:&format errorDescription:&errorString];
Creating it from a JSON just requires using one of the readily available JSON libraries. Once you have the dictionary, save it:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *file = [NSString stringWithFormat: @"%@%@.plist", [paths objectAtIndex:0], @"MyPlistBaseName"];
[plist writeToFile: file atomically:NO];
And re-load it later:
NSDictionary *plist = [[[NSDictionary alloc] initWithContentsOfFile: file] autorelease];
However, If you encapsulate the access to this data in a singleton, then you can worry about optimizing the implementation if speed / memory becomes an issue. Just treat that signleton as the 'owner' of the data, with methods like count
, getGroup(0)
(to return a block of 25), etc. Then you can hide all the implementation details inside the object.
精彩评论