开发者

Core Data and REST

I'm developing an iOS application which deals with constantly changing data which is read and written to through a web-based REST API. I want some data to persist throughou开发者_如何学编程t the app's lifecycle, but I also want it to be reset and reloaded when the user quits the app.

Should I user Core Data for this? If not, is there a simple way to persist data throughout the app's lifecycle (eg. a singleton class?). What would you guys recommend? Are there benefits to using Core Data? I want to avoid Core Data if possible as it's a pain to set up.

Thanks in advance


Core Data isn't about saving data, Core Data is about modeling data during runtime. Persistence is just an option. If you have no interest in persisting the data to disk, just use Core Data with an in-memory store.


A singleton class is how I would do it...except, iOS makes things a little bit more complicated because it will quit your app if memory runs low. In that instance if you want to persist data you will need to write it out, and Core Data is a good choice for that. If, however, you feel you can just reload everything if your app gets evicted, then feel free not to mess with it.


Usually iOS developers use plist files when there is too little data involved to spend the time and effort to set up a Core Data context. This format resembles JSON in that it only allows array, dictionary, number, string and date objects. To save a dictionary or an array as a plist file, simply call the writeToFile:atomically: method, and read It with the initWithContentsOfFile: initializer.


As mentioned by TechZen, CoreData is about establishing your model. You can then go ahead and override how your model gets populated and persisted: locally, over the network (REST), etc.

A little "overhead" at first when designing your model, for sure, but then later you can change how your model is loaded/persisted (e.g. via a Singleton/in-memory to over-the-wire) without having to change the rest of your app. So basically your View Controllers are isolated from these changes.


There is an open source project that allows Core Data to be tied to a REST API: http://restkit.org/

You could also look into Mobile CouchDB. Although it currently does not tie into Core Data, there are plans in the near future to support it. Personally, Core Data can add too much complexity to some problems, and mapping JSON documents to ObjC objects isn't very hard. CouchDB allows you to sync data between your device and server easily.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜