问题
I loaded XML data (including images, text,...) from server and display that data on iphone screen.
How can i cache data to re-load that screen when i visit that screen other time. It will be faster . (dont need re-load XML data again)?
Thank you.
回答1:
Use EGOcache API. Just import the EGOCache.h file in your class
- (void)setObject:(id<NSCoding>)anObject forKey:(NSString*)key;
-(void)setObject:(id)anObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
-(id)objectForKey:(NSString*)key;
Use the first method for set your data with a forkey. If you want secify the cache time then use the second method.
Edit
EGOCache will save your data permanently, See here to for usage of NSCache which is incorporates various auto-removal policies.
回答2:
you can store XML file itself to documents folder (or some subfolder) after downloading the data from server. If you have parsed XML file into arrays and dictionaries, you can convert data to plist file and store that on your iphone itself. Look at the NSData method:
- (BOOL) writeToFile: (NSString*)path atomically: (BOOL)useAuxiliaryFile;
来源:https://stackoverflow.com/questions/3707718/how-to-cache-data-in-ios