Learn Objective-C on the Mac - Mark Dalrymple [82]
Build and run your app, and now the green bar changes when you drag one of the other sliders. Awesome!
In Conclusion
In this chapter, we’ve covered a pretty wide range of material about Core Data. You’ve learned about creating a model file in Xcode, and about using the combo of Core Data and Cocoa Bindings to quickly put together a decent GUI. You’ve also gotten some idea of the underpinnings of how Core Data stores its data, and learned some basics about implementing your own business logic. In Chapter 8 you’ll build on this knowledge, learning how to complete your data models by adding relationships.
Chapter 8
Core Data Relationships
In the last chapter, you learned a lot of the basics about how Core Data works, but worked with an extremely simple data model, which contained just a single entity. In this chapter, we’re going to show you how to extend your data model to contain multiple entities, and how to define relationships between those entities. You’ll also see how to create a GUI that shows these relationships and lets you edit them. Along the way, you’ll catch your first glimpse of how Core Data deals with multiple, incompatible versions of your data model, and how it can migrate a data store from one model version to the next.
We’ll show you all of this while extending the MythBase project from Chapter 7 to include some data that’s often overlooked with discussing mythical heroes and gods: the legendary bands they performed in! The night that Achilles and the Grecian Formula shook the Old Parthenon Ballroom to the ground is the stuff of legends, and who could forget The Four Norsemen’s farewell concert at the Clontarf Supper Club? The enhancements we’ll make to MythBase in this chapter (see Figure 8-1) will help you keep track of this crucial information.
We’ll start out by adding all the new entities and relationships we want to our data model, then go on to setting up the GUI that will let us edit everything.
To prepare for the rest of this chapter, go the Finder and make a copy of the last chapter’s final MythBase project directory, then go into the new directory and double-click MythBase.xcodeproj to open it in Xcode.
Figure 8-1. The new and improved MythBase application we’ll create in this chapter
Modeling New Entities and Relationships
We’re going to be adding three new entities to MythBase: MythicalBand, MythicalGig, and MythicalVenue. We’ll also add relationships between several of them. When we’re done, our data model will look like Figure 8-2.
Figure 8-2. Our new data model
Model Versioning and Migrations
Before we get started making changes to the data model, there’s one important issue that needs to be addressed: migration. We’re not talking about people moving from one country to the next, or birds flying south for the winter. We’re talking about your data. If you’ve ever developed a database application, you’ll be familiar with the concept of “migrating” data from one deployment version to the next. This typically involves writing some sort of script that makes changes to the database structure itself (adding, deleting, or altering tables and columns) and populates new fields with appropriate values. With Core Data, you don’t exactly write a script, but the same idea is in use. In your data store, Core Data saves some metadata about the structure of the data model. When your app runs, Core Data tries to read from the data store. If it determines that the app has a new version of the data model with a different structure, it will automatically update the data store to match the latest data model. Let’s see how this works.
Preparing for Multiple Model