How to cache data in ios

做~自己de王妃 提交于 2019-12-10 23:16:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!