Learn Objective-C on the Mac - Mark Dalrymple [85]
You’ve now created all the relationships we’re going to need for the new version of MythBase, and your model should look similar to what you saw in Figure 8-4, a couple of pages ago.
Creating a Simple Migration
Assuming that you’ve previously gone through Chapter 7, and used that version of MythBase to save some data, running the new version at this stage will give you some problems. You’ll see some error messages about mismatched data store versions when you start up, and you’ll get nothing but an empty table view where the MythicalPerson objects you created previously should be. And if you add some new objects and try to save them, you’ll just get more errors! The reason for this is that the current version of your app’s data model differs quite a bit from the version that was used to save the data previously, and Core Data is smart enough to detect this, let our app delegate know about it, and prevent us from overwriting the old version with new data. Not only that, but it’s also smart enough to automatically “upgrade” an existing data store to work with our new model version, but only after we create a migration.
Core Data migrations are centered around files called mapping models, which are created and configured right in Xcode. For simple things like adding new empty tables and optional attributes, no particular configuration is necessary, we just create a mapping model and its default configuration does all the work. If you’re creating a required attribute, or new tables that need to be populated with existing data, you can define some mappings in Xcode that will populate values for you. For more complex data manipulations, you can write Objective-C code to handle aspects of your migration, and this code will be automatically run when the migration occurs. In this example, we’ll get by with just a simple mapping model without any extra configuration at all.
Right-click (or control-click) the Models group in the Xcode navigation pane, and select Add➤New File from the contextual menu. In the window that comes up, choose Mapping Model from inside the Cocoa section, and click Next. Name your new mapping model MythBase_1_to_2.xcmappingmodel and click Next. The next window shows you an overview of your project’s groups and files, similar to the main navigation pane, and asks you to set the source and destination models. Click your way into your multi-version model file (inside the Models group), select the first version and click Set Source Model, and then select the new version and click Set Destination Model (see Figure 8-6).
Figure 8-6. We’ve specified the versions we want to migrate between.
Then click Finish, and you’ll be taken back to your Xcode project window, with the new mapping model selected in the navigation pane. The editor for the mapping model is a complicated-looking set of views that we’re not really going to explore at this point. It gives you a set of views that are somewhat reminiscent of the upper part of the Xcode data model editor (see Figure 8-7). On the left you have a table showing all entities, to the right of that you have a table showing all the selected entity’s properties, both attributes and relationships, and a third view shows the details for the last selected entity or property.
Figure 8-7. Xcode’s view of a mapping model, showing mapping details for the MythicalPerson entity
Feel free to click around on the various entities and properties to see what the configuration GUI looks like, but stop short of editing any of the configuration options! The