Reputation: 2191
When calling the addPersistentStoreWithType
method of a NSPersistentStoreCoordinator
object, what is happening exactly ?
By looking at the examples on the web, I see that calling this method will add the persistent store at the given URL to our persistent store coordinator, am I right ?
My main question is about what happens if :
Thank you !
Upvotes: 1
Views: 1124
Reputation: 11
Although not fully describe, but Yes, this method "will add the persistent store at the given URL to our persistent store coordinator"
More than that, it create one or more data file with extension .xml / .sqlite / .bin / custom
For easier illustration of how Persistent Store & Persistent Store Coordinator work together, let think of object as the TABLES (although it's technically not tables in most of the times !)
Managed Object Model (MOM) is the Framework / Master Template (any words!) Table which describe ENTITIES DESCRIPTION including only 3 matters: (Entities) Name, (Entities) Properties and (Entities) Relationship
Persistent Object Store (POS) table is an Object presence of Persistent Store' data file(s). It is called Managed Object.
Persistent Store Coordinator (PSC) do amazing tasks by combining MOM and POS into a well formatted, well structure data. Technically, PSC retrieve data from POS and structure them into MOM's defined Format Tables.
Once calling method addPersistentStoreWithType
, the PSC will do a reverting process: Referring to MOM and do extracting POS from Object into Persistent Store's data file(s) and save as Script, Binary or certain storage methods. Since Xcode 7, everytimes you call anyInstanceOfNSManagedObjectContext.save() throw
, the compiler automatically take the Persistent Store saving for you. This means unless you have more than one Persistent Store, the addPersistentStoreWithType
is not a must.
Hope this helpful to you
Upvotes: 0
Reputation: 21536
If there is no store at the given URL, CoreData will create it, adding the relevant tables according to the managedObjectModel associated with your persistent store coordinator.
If there is a store at the given URL, CoreData will check to ensure that it is consistent with the model. If not, then (by default) CoreData will throw an error:
The model used to open the store is incompatible with the one used to create the store
This is a common problem when people first begin building and designing a CoreData app. The easiest solution at that stage is just to delete the app from the simulator (which removes the existing store) and re-run. But that's not a viable solution for production apps, which need to accommodate design changes to the model whilst preserving the user's data. To address this, CoreData supports multiple versions of a model and has an extensive API for migrating from one model version to another. Refer to the Core Data Model Versioning and Data Migration Guide for full information.
For many model changes, you can use a "lightweight migration". See this answer for a good example of using lightweight migration.
Upvotes: 2