This post is a little memo I made for myself. Hope it will be useful to you too. You can find the sample code in Apple’s Core Data Programming Guide.
Here are the steps you take to initialise the Core Data Stack.
- Initialise Managed Object Model with modelURL. The model file may look like “DataModel.momd” and is located in the main bundle of your project.
- Initialise Persistent Store Coordinator with Managed Object Model from step 1.
- Initialise Managed Object Context. You can save it as a property, to access it later.
- Take Persistent Store Coordinator from step 2 and set it as a Persistent Store Coordinator of the Managed Object Context from step 3.
- Create storeURL. Your store can be named something like “DataModel.sqlite”, and should be placed in the Documents directory.
- Using the storeURL from step 5 add the store to the Persistent Store Coordinator from step 2.
That’s it. You should end up with something that looks like this: