Search code examples
iosweb-servicescocoa-touchcore-dataios5

Explanation of NSIncrementalStore in plain english


I've been seeing NSIncrementalStore popping up as I've been researching the best ways to interact with a web service using core data.

After reading an article by Drew Crawford, a programming guide, a class reference and this tutorial a couple of times each I'm still struggling understanding what NSIncremental store is, why and when you would use it.

Could someone please explain it?

Edit
after reading mundi's answer I can see some further context is required. I'm looking into using core data in conjunction with a web service I am building. I am attempting to find the best way to store the users information locally on the device and post to web service when there is a connection. My lack of knowledge on core data prompted my research, but I was unable to fully comprehend the usefulness of NSIncrementalStore.


Solution

  • NOTE: this API was bleeding-edge when I wrote this in 2012 and details have changed. Feel free to update this if you wish. I'm not working on any Cocoa/ObjC projects at the moment so I am not a good person to keep this up-to-date, unfortunately. It does seem that the overall gist is correct.

    Core Data provides a set of tools that help manage object persistence, i.e. the ability to save and then fetch back sets of objects (NSManagedObject) from some kind of storage.

    When you work with Core Data objects, you do so using an NSManagedObjectContext, which you get from an NSPersistentStoreCoordinator. The PSC in turn talks to one or more NSPersistentStore subclasses, which handle the actual operations on the store. (Think create/read/update/delete against a database.)

    Core Data supports two primary kinds of store: NSPersistentStore and NSAtomicStore. A persistent store can be thought of as a database: you can incrementally save, update and fetch arbitrary sets of records from it. An atomic store is an 'all or none' representation of an object graph. It is intended to be an in-memory representation of a structured file.

    The store types that Core Data comes with are:

    • NSSQLLiteStoreType (NSPersistentStore)
    • NSInMemoryStoreType (NSPersistentStore)
    • NSXMLStoreType (NSAtomicStore)
    • NSBinaryStoreType (NSAtomicStore)

    NSPersistentStore is explicitly forbidden to be subclassed, so until now, there has been no way to create your own non-atomic store backend. That is, if you wanted to persist and query representations of your objects piecemeal as opposed to in one big lump ('load the entire graph', 'save the entire graph') you've been out of luck. Until iOS5 introduced NSIncrementalStore.

    NSIncrementalStore is an abstract class (descended from NSPersistentStore) whose methods you implement to provide an adapter between a data store that you control and the world of Core Data. You can use it to wrap a remote API, or if you were so inclined you could wrap something like NULevelDB or NanoStore (though I'm not sure why you'd do that).