开发者

Objective C Data Caching on iOS

开发者 https://www.devze.com 2023-02-27 03:26 出处:网络
I am pulling data from an API and then building out my data objects with it.I want to cache this data for the future.I have been storing the string from the 开发者_开发百科api in NSUserDefaults and th

I am pulling data from an API and then building out my data objects with it. I want to cache this data for the future. I have been storing the string from the 开发者_开发百科api in NSUserDefaults and then re-parsing it each time the app loads. This works, but it really seems like the wrong way to go about it.

How can I do this?


Have you noticed the NSCache?

An NSCache object is a mutable collection that stores key-value pairs, similar to an NSDictionary object. The NSCache class provides a programmatic interface to adding and removing objects and setting eviction policies based on the total cost and number of objects in the cache...


Personally I'm quite fond of the EGOCache classes, I use them quite a lot in my projects:

https://github.com/enormego/EGOCache

The classes are easy to use, I used to have my own classes with a similar design, but these are just more well-rounded, so I decided to stick with them (don't wanna reinvent the wheel).


There are many different solutions to this problem and there is no "right" way to do it. A few popular options are:

  • Core Data - Apple's framework for persistence. Very performant, but more difficult.
  • SQLite - Fast and flexible, but bare bones.
  • Plists - Basically writing a file to disk, you have to read and write manually.
  • NSUserDefaults - The lightest weight "key-value" option.

I would encourage you to read up on all four and see which one works best for you.


I vote Core Data


What type of data? If its text/string bases SQLLite would probably be the best.


I'd store the computed/parsed data in either a Core Data store, or in NSData flat files in your application's Documents directory. You're correct that storing that in NSUserDefaults and then re parsing feels a little overkill.

0

精彩评论

暂无评论...
验证码 换一张
取 消