开发者

SQLite Persistent Store as a cache

开发者 https://www.devze.com 2023-04-12 03:42 出处:网络
I\'ve got an idea to build custom Persistent Store that would basically work as a cache. I.e. objects that are available from local SQLite Store would be retrieved from it and those that are not avail

I've got an idea to build custom Persistent Store that would basically work as a cache. I.e. objects that are available from local SQLite Store would be retrieved from it and those that are not available would be retrieved from network and then put into SQLite.

The problem is how to wire this in Core Data mechanism so that no change in application logic is required. The caching store needs to behave like a proper Persistent Store. At the moment my i开发者_StackOverflow中文版dea is to extend SQLite Persistent Store.

So the question is where should I begin? :) Is this a sane idea or is my understanding of Core Data totally wrong and things like that shouldn't be done in a respectable app? Has anyone done anything similar before?


Caching locally is certainly valuable in devices where you're not consistently connected. Apps are also much more responsive working off local stores.

One option outside of coding your own is restkit (restkit.org).

From their page:

Core Data support. Building on top of the object mapping layer, RestKit provides integration with Apple’s Core Data framework. This support allows RestKit to persist remotely loaded objects directly back into a local store, either as a fast local cache or a primary data store that is periodically synced with the cloud. RestKit can populate Core Data associations for you, allowing natural property based traversal of your data model. It also provides a nice API on top of the Core Data primitives that simplifies configuration and querying use cases.


Remember that CoreData has significant performance and caching mechanisms already built in. When interacting with fetched results controllers, CoreData faults and populates objects automatically, performs caching under-the-hood, and also returns objects in batches that you can specify the size of.

If you have a very particular scenario, perhaps you could improve your performance by redirecting your reads through a self-written cache. But for most purposes, just using CoreData as it is intended to be used automatically gives you the benefit of some pretty sophisticated stuff.

0

精彩评论

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

关注公众号