By the time we are finished, Imager will have a dozen or so data entities. For now, we’ll create simple versions of two of them.
The first is the Chord, which will eventually contain a background color and a collection of colored objects that move about Imager’s surfaces. Each of those objects is referred to as a lumi (a Logical Unit for Manipulating Images). We will define lumis in a later module. For now, we will create enough of the chord to let us connect Imager with CoreData and with Metal, and to define Chordsets. In doing so we’ll learn the most important things we’ll need to know about enabling Imager’s data to persist.
Press Cmd+N to create a new file, then select Data Model (in the Core Data subsection) and press the Next button. Name it Imager.xcdatamodeld. Press the Create button.
Press the Add Entity button twice and rename the entities Chord and Chordset.
Now, select the Chord entity and press the + button at the bottom of the Attributes section four times. This will create four attributes with the Undefined type. Change their names and types to these:
- “id”, with the type UUID
- “position”, with the type Integer 16
- “bpm”, with the type Double
- “bgColor”, with the type Integer 16
The “id” will be used to insure that each Chordset is uniquely identifiable in our SwiftUI implementation. The “position” will hold a number from 0 to 7, indicating where in the Chordset that particular Chord resides. The “bpm” stands for beats per minute and will be used to determine how rapidly changes in the Chord take place. And, “bgColorIndex” will be used temporarily to get some variety going among the chords. It will eventually be replaced by a proper background color model.
Select the Chordset entity and press the + button in the Attributes section three times. Change the names and type of these attributes to these:
- “id”, with the type UUID
- “title”, with the type String
- “creationDate”, with the type Date
Again, “id” insures that each Chordset is uniquely identifiable. “Title” is a description that allows users to name and identify Chordsets and the “creationDate” attribute will be used to order chordsets in a list.
Chords belong to Chordsets; indeed, that is why we are creating these two data entities now. So, let’s create a relationship between them in the data model. With the Chordset entity selected, press the + button under the Relationships header. Rename the new relationship “chords”. Change the Destination from “No value” to “Chord”.
Now select “chords” under Relationship again. Be sure that the data model inspector is open (if it is not, pull down the view menu and select Inspectors/Data Model). Where it says “Type: To One” change that to “To Many”, since a Chordset can have multiple Chords in it.
We will make this relationship reciprocal. Each Chord belongs to one Chordset. So under entities, select Chord. As before press the plus at the bottom of the Relationships section. Rename the relationship “chordset” and choose Chordset as its Destination. Leave the Type as To One.
Set the Inverse of this relationship to “chords”. The system will take care of setting the inverse of the chords relationship that Chordset holds.
One last thing remains. Under entities select Chordset, then under Relationship select chords. Over in the Data Model Inspector panel you should see a line labeled “Delete Rule”. Replace “Nullify” with “Cascade”. This will have the effect that when we delete a chordset, that deletion will cascade down to delete the chords associated with it.