I want to add a new Core Data Entity in my project. Do I need to add a new core-data Model Version for this or it will work within existing version? The entity is independent and it will have no relation with other previously defined entities.
2 Answers
Indeed, it requires adding a new model version. However, adding a new entity can be handled by a lightweight migration, so at least you don't have to create a mapping model/write custom migration code.
UPDATE:
From the article about lightweight migration:
In addition, Core Data supports: ... Changing the entity hierarchy
- You can add, remove, rename entities
- You can create a new parent or child entity and move properties up and down the entity hierarchy
- You can move entities out of a hierarchy
- You cannot, however, merge entity hierarchies; if two existing entities do not share a common parent in the source, they cannot share a common parent in the destination
And if you don't use versioning, you'll get an error like this:
Error Domain=NSCocoaErrorDomain Code=134100 "(null)" UserInfo={metadata={ NSPersistenceFrameworkVersion = 641; NSStoreModelVersionHashes = { TestEntity1 = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( "" ); NSStoreType = SQLite; NSStoreUUID = "F16AD345-02FE-4E94-B11E-3BC337B16568"; "_NSAutoVacuumLevel" = 2; }, reason=The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary { metadata = { NSPersistenceFrameworkVersion = 641; NSStoreModelVersionHashes = { TestEntity1 = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( "" ); NSStoreType = SQLite; NSStoreUUID = "F16AD345-02FE-4E94-B11E-3BC337B16568"; "_NSAutoVacuumLevel" = 2; }; reason = "The model used to open the store is incompatible with the one used to create the store"; }
This looks like a good reason to use versioning to me)

- 7,398
- 2
- 30
- 60
-
As mentioned in the doc, light migration only takes place when there is a change in attributes in an entity. I couldn't find anything related to adding a new Entity without any change in existing ones. Can you please elaborate over why this is necessary? – atulkhatri Mar 28 '16 at 12:52
-
@atulkhatri, I've updated my answer. TL;DR: otherwise you won't be able to initialize the Core Data stack (see the attached error message). – FreeNickname Mar 28 '16 at 13:21
-
I am not getting any crash with above exception, may be because I have enabled light weight migration in my AppDelegate. I will probably add a new model version just to be on the safer side if I don't get any rigid reason why it's not necessary entirely, knowing my entities are independent of each other. – atulkhatri Mar 28 '16 at 13:38
-
1@atulkhatri, does your persistent store contain any objects? If it doesn't, the app doesn't crash. Try adding some objects, saving the context, and then changing the model (without creating a new version) and reloading the app. It should crash. – FreeNickname Mar 28 '16 at 13:54
Adding a new Entity to your existing model will still work fine with your project, however if your app is already live on the App Store, you should add a new model version based on the current one and add the new Entity to this instead, so that lightweight migration can be handled.
If the project is still in development though, adding or changing to the current model is just fine. You may need to delete and re-install to your device with changes like this, though as I say, if it's still in production this shouldn't matter to you.

- 4,078
- 3
- 27
- 48
-
As replied to @FreeNickname, I couldn't find anything related to addition of new entities in lightweight migration. My app is live on the App Store. Are you suggesting versioning just to be on the safer side? – atulkhatri Mar 28 '16 at 12:56
-
@atulkhatri, If it's for an App Store update, then yes, I'd recommend adding a new model version – Jim Tierney Mar 28 '16 at 13:09