Search code examples
ioscore-dataentities

Core Data - Multiple entities


Fellows!

I have an app using Core Data where I created one Entity (let's call "Entity1") and one attribute. I implemented it and the app was running perfectly. It was reading, writing and deleting datas.

So, I created a second entity ("Entity2") and I didn´t implement any code related to "Entity2". However, neither "Entity1" nor "Entity2" was running anymore.

After that, I deleted "Entity2" and the app started working perfectly again.

Could you tell me if there is any secret to create multiple entities in Core Data.


Solution

  • It is because your model file (xcdatamodel) and your existing database are not match. Try to delete your app in simulator or dev device, then build and run again, it should be OK.

    And, this is just for development. When you released your app and then want to change the database model, there is impossible to ask your users to "delete your app and reinstall again". When this time, you need a Core Data Model Versioning and Data Migration. But that is another story.

    I think you should begin from the basic of Core Data. Refer to this tutorial to get familiar with it. :)