Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data with in-memory store

I want to use Core Data as a cache for a larger dataset on a database server. Not all data will be in memory.

When thinking about this further 2 questions came to mind:

  1. Can faulting (for example for 1-n relationships) be used with the in-memory persistent store, and if so, how do you a catch a fault firing?

  2. A Core data managed object context has a staleness interval. Is this also applicable to a in-memory store?

Or should I use a NSAtomicStore for this purpose?

like image 654
diederikh Avatar asked Nov 07 '09 15:11

diederikh


People also ask

Where is Core Data data stored?

The persistent store should be located in the AppData > Library > Application Support directory. In this example you should see a SQLite database with extension . sqlite. It is possible that you don't see the persistent store in the Application Support directory.

What is Core Data storage?

A data center -- also known as a datacenter or data centre -- is a facility composed of networked computers, storage systems and computing infrastructure that organizations use to assemble, process, store and disseminate large amounts of data.

How does Core Data save data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

What types of stores does Core Data support?

Core Data provides four store types—SQLite, Binary, XML, and In-Memory (the XML store is not available on iOS); these are described in Persistent Store Features.


1 Answers

Your first question suggests you misinterpreted the intention of NSInMemoryStore type persistent stores. They are the persistent store part of the Core Data stack. Faulting is something that happens when you bring instances into a managed object context; a fault is created which fires and populates itself from the NSPersistentStoreCoordinator's cache or the underlying persistent store when fired. An in-memory store does not change the faulting relationship. Obviously, it won't really help your problem, however since you'll have to persist all the data to memory. In-memory stores are really appropriate for (1) testing (they're fast) and (2) scratch core data stacks in which you want to use Core Data's object graph management without having to persist anything to disk.

In response to you second question, the answer is YES. The staleness interval applies to the context, not to the persistent store.

So, is Core Data appropriate for caching data from a remote database server? Not really. Although Bill Bumgarner (an Apple engineer) has hinted that it's possible, I've found it much easier in my own code to separate the caching from the Core Data object graph management. It's still very nice to use Core Data to manage an object graph and for ease of binding to a Controller/UI layer(s). So my strategy is pull data from the database server and cache it in my own data structure (the libcache and NSCache in OS X 10.6 might make a very good starting point). Then decide what you want in your object graph and migrate that into a Core Data stack (backed by an in-memory persistent store). You'll have to handle change notification or polling from the database server yourself. When the data from the database changes (or the user query changes, etc.), I just tell all editors to finish editing, then wipe the context and rebuild it from the (possibly) updated cache.

like image 61
Barry Wark Avatar answered Oct 05 '22 17:10

Barry Wark