'Xcode Swiftui -- when Core Data entities are deleted, app crashes on My Mac(designed for iPad)

Whenever entity changes to CoreData are made (especially on deletion of an entity)-- other than adding attributes, the simulator will crash. Deleting Derived Data does not help...I am assuming because the Simulator stores data from previous builds. On ios Simulators it can be solved by deleting the app instance on the simulator; but using the "My Mac (designed for iPad)" proxy this cannot be done... or am I missing something? The only thing that helped was to find the app sqlite file and delete that (not easy to find), which forces the project to reset everything. Any other suggestions, or is this a bug?



Solution 1:[1]

From your description it sounds like you're editing the data model, in the Xcode model editor. Basically, you can't just do that unless you configure your app to migrate existing data from the old model to the new one. When the app launches, Core Data needs to match data it already has (the persistent store) to the data model the app has. If they don't match and Core Data can't figure out how to automatically convert the data, the app crashes.

For relatively simple changes, Core Data can figure out what to do and takes care of things. For other changes, it can't do that, so it's up to you.

If you don't need to keep old data (like, you're still developing the app and you only have test data), then what you're doing is normal. Delete the older version of the app and start fresh. There's no need to update your test data to match the new model.

If you do need to keep old data, you need to create a new version of the data model but keep the old one around. Core Data knows how to handle multiple versions of the data model; you'll tell it which one is current, and the others will all be old versions. Then, depending on what exact changes you made in the model, you can migrate the data to the new version. This is a whole topic on its own and if that's the case, please post a new question with the exact details of your changes and someone may be able to help.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tom Harrington