

By running the application for the first time, Core Data automatically created a persistent store based on the data model of the project. What does that mean? When you downloaded or cloned the project from GitHub, I asked you to run the application to make sure everything was set up correctly. Core Data tells us that the data model is not compatible with the data model we used to create the persistent store. We are closing in on the root of the problem. reason = "The model used to open the store is incompatible with the one used to create the store"

The following line tells us what went wrong. Run the application again and inspect the output in the console. The goal of this tutorial is to prevent that adding the persistent store to the persistent store coordinator fails. If adding the persistent store to the persistent store coordinator fails, the application invokes the fatalError() function, which causes the application to terminate immediately. Open CoreDataManager.swift and inspect the implementation of the persistentStoreCoordinator property. Finding the Root Causeįinding out the cause of the crash is easy. In this tutorial, we find out what happened, how we can prevent it, and how we can modify the data model without running into a crash. Did that crash take you by surprise? That's what happens if you mess with the data model without telling Core Data about it. Run the application in the simulator or on a physical device. It's time to verify that everything is still working. This is what the data model now looks like. Remember that the inverse relationship of the lists relationship is automatically set for us by Core Data. Name the relationship user and set the inverse relationship to lists. Select the List entity and create a To One relationship with the User entity as its destination. In the Data Model Inspector, set Type to To Many. Add two attributes to the User entity:Īdd a relationship, lists, to the User entity and set its destination to the List entity. Add an entity to the data model and name it User. Open the project's data model by selecting Lists.xcdatamodeld in the Project Navigator. Run the application in the simulator or on a physical device to make sure everything is set up correctly. Modifying the Data Modelĭownload or clone the project we created in the previous tutorial and open it in Xcode. In this tutorial, we take a look at how Core Data helps us manage changes of the data model and what pitfalls we absolutely need to avoid. Core Data handles changes pretty well as long as you play by the rules of the framework. The data model, for example, grows and changes.

An application that grows and gains features also gains new requirements.
