ioscore-datansmanagedobjectcontextnspersistentstorensmanagedobjectmodel

Programmatically Setting Up Core Data - Where Does NSManagedObjectContext Come From?


I want to set up Core Data entirely programmatically. I've defined a database schema in the form of an NSManagedObjectModel and added some entity definitions in the form of NSEntityDescription's as is done at the bottom of this page. All is well with that.

I then created an NSPersistentStoreCoordinator using its class method initWithObjectModel: and passing in the aforementioned schema. At this point I don't know what to do.

I assume the next step is to somehow get an NSManagedObjectContext, because as I understand it, it's like a DB scratchpad and is required for most everything I want to eventually do (create,read,update,delete records, etc). What's the next step (keeping in mind no gui tools), and if it's getting a context, how do I do that? Also, if anyone knows any blog posts / resources for doing this sort of thing entirely programmatically, that'd be great.


Solution

  • Here's how to setup the NSManagedObjectContext:

    [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    

    A good step-by-step guide with explanation can be found here:

    http://www.objc.io/issue-4/full-core-data-application.html