Does adding a new independent entity in Core Data require model versioning?

心已入冬 提交于 2021-01-28 04:47:41

问题


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.


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/36261757/does-adding-a-new-independent-entity-in-core-data-require-model-versioning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!