318 ❘ CHAPTER 15 DATA MODELING Adding Fetch Requests Fetch requests are simply predefi ned predicates. Your application can use them in a variety of ways. When you add a fetch request, the property list display changes from displaying regular properties to listing only fetch requests. To switch between two, select the desired view using the disclosure triangle at the bottom of the properties list. See the “ Data Model Browser ” section for more details. DATA MODELING INTERFACE The interface used by data modeling is so similar to the interface used for class modeling that it would be a waste to reiterate it all here. Instead, this section just points out the differences between class modeling and data modeling. Before reading this section, you should familiarize yourself with the class modeling interface if you have not already done so. Class modeling was described in detail in Chapter 14. Data Model Browser The data model browser lists the entities on the left, the entity properties in the middle, and the details pane on the right, as shown in Figure 15 - 7. FIGURE 15-7 FIGURE 15-6 c15.indd 318c15.indd 318 1/21/10 3:52:08 PM1/21/10 3:52:08 PM Download at getcoolebook.com Selecting an entity in the Entity list displays the properties or fetch requests in the center list. Selecting multiple entities displays a combined list of all properties of the selected entities. The use of the + and – buttons has already been described in the “ Creating Properties ” section. The disclosure triangles below each list control how each list is displayed. Similar to the class browser, the Property list has the choices of Show All Properties, Show Attributes, Show Relationships, and Show Fetched Properties. These four choices let you display all standard property types, or limit the display to a single type. The fi fth choice, Show Fetch Requests, shows only fetch requests. The formats of the fetch request and property lists are different and are mutually exclusive; the browser can ’ t display both properties and fetch requests at the same time. Details Pane Views The details pane shows the settings for the selected entity or property. If you have multiple entities or properties selected at once, the details pane displays the settings common to all of those items, if there are any. Changing a setting that applies to all of the selected items alters all of those items at once. The details pane has four different inspectors that you select using the small tabs in the upper - right corner of the pane. Figure 15 - 8 shows the four different panes. From left to right, they are General, User Info, Confi gurations, and Synchronization. FIGURE 15-8 General Pane The General pane displays the settings and controls for the selected entity or property. The format of this pane varies depending on what items are selected in the browser and even on what settings have been selected. User Info Pane The User Info pane lists the user info dictionary attached to an entity or property. The dictionary cannot be attached to fetch requests. A user info dictionary is simply a list of key - value strings associated with the entity or property. These values are stored in the data model and can be retrieved at run time by your application for whatever purpose you need. Confi gurations Pane Confi gurations are named collections of entities and apply only to entities. Using confi gurations, a data model can contain many different combinations of entities. Your application can then selectively load a data model that contains only the set of entities that it needs. For example, an Data Modeling Interface ❘ 319 c15.indd 319c15.indd 319 1/21/10 3:52:09 PM1/21/10 3:52:09 PM Download at getcoolebook.com 320 ❘ CHAPTER 15 DATA MODELING application for professional photographers might have entities for Image, Thumbnail, Keyword, ModelRelease, CopyrightOwner, and PriceSchedule. That same application, running in “ amateur ” mode, might only want a data model that includes Image, Thumbnail, and Keyword. This can be accomplished in a single data model by creating two confi gurations, “ Professional ” and “ Consumer, ” including all of the entities in the “ Professional ” confi guration, but omitting ModelRelease, CopyrightOwner, and PriceSchedule from the “ Consumer ” confi guration. Create new confi gurations by clicking the + button at the bottom of the pane. Select one or more entities and put a check mark next to the confi gurations they belong in. To delete a confi guration, select it and click the – button or press the Delete key. You ’ ll also fi nd versioning, migration, and miscellaneous storage settings in the Confi gurations pane. The Renaming Identifi er is described later in the “ Migrating Data Schemas ” section. Synchronization Pane The synchronization settings let you integrate your Core Data model with Sync Services, a framework for synchronizing data changes with remote data sets. You can learn more about Sync Services in the Sync Services Programming Guide included in the Xcode documentation, or online at http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/SyncServices/ . Data Model Diagram Nodes in a data model represent entities. The compartments of entity nodes are Attributes, Relationships, and Fetched Properties (see Figure 15 - 9). The Fetched Properties compartment appears only if fetched properties have been defi ned for the entity. Fetch requests are not visible in the data model diagram. FIGURE 15-9 c15.indd 320c15.indd 320 1/21/10 3:52:09 PM1/21/10 3:52:09 PM Download at getcoolebook.com Data Modeling Interface ❘ 321 Lines between entities describe relationships and inheritance. The shapes of the arrowheads indicate the kind of relationship, as listed in the following table: LINE RELATIONSHIP Single arrowhead To - one relationship. The arrow points to the destination entity. Double arrowhead To - many relationship. The arrow points to the destination entity. Hollow arrowhead Inheritance. The arrow points to the superentity. If two relationships have been fl agged as being inverse relationships, the data model diagram represents both relationships as a single line with two arrowheads. This greatly improves the readability of the diagram. Inheritance and unidirectional relationship lines begin at their source and point abstractly toward the destination entity. For inverse relationships, Xcode draws the line precisely from one complementary relationship property to the other. Selecting a node or attribute in the diagram selects the same in the browser. Selecting a relationship line in the diagram selects the relationship in the browser. If the line represents two inverse relationships, both relationships are selected along with the entities that contain them. Like class modeling, you can customize the appearance of nodes. You cannot, however, fi lter the entities or properties that are displayed. Tools The Arrow, Magnifying Glass, and Hand tools work exactly as they do in class modeling. There is no Notes tool in data modeling. Use the Line tool to create relationships between entities. Select the Line tool and drag from one entity to another. A new relationship is created in the entity where the drag began. To create a relationship between an entity and itself, click the entity. Follow the steps in the earlier “ Relationships ” section for editing its details. Duplicating Entities and Properties You can copy entities and properties to the clipboard. From there, you can paste them into the same or different data models. You can also duplicate entities in the data model diagram by holding down the Option key while dragging an entity to a new location. Duplicated entities are given names with numeric suffi xes. Predicate Builder The predicate builder allows you to construct predicates — logical expressions used to fi nd, select, or fi lter data — graphically. The predicate editor is based on the Cocoa Predicate framework. You can fi nd both an introduction and a complete description of predicates in the Reference Library under Cocoa ➪ Data Management ➪ Predicates Programming Guide, or you can fi nd it online at http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Predicates/ . c15.indd 321c15.indd 321 1/21/10 3:52:10 PM1/21/10 3:52:10 PM Download at getcoolebook.com 322 ❘ CHAPTER 15 DATA MODELING You can invoke the predicate builder, shown in Figure 15 - 10, from a variety of places. It is even used in class modeling to determine what classes and members are displayed. The predicate builder is context - sensitive. That is, the pre - assembled set of values used to construct the expression will be garnered from the entity or its destination, as appropriate. Using the predicate builder will be easier and more productive if you have fi rst defi ned the entire context surrounding the predicate before you begin editing it. Specifi cally, you should defi ne the types of attributes and any relationships between entities before trying to edit a predicate. FIGURE 15-10 Simple expressions are constructed from keys (which specify the variable or attribute), operators, and values. You can combine simple expressions using logical operators. The textual form of the expression in Figure 15 - 10 is year > = 3 AND (ANY classes.department.name == “ Zoology ” OR ANY classes.department.name == “ Anthropology ” OR ANY classes.department.name == “ Biology “ ) AND ANY classes.level > 400 . Simple Expressions The key menu selects the attribute you want to compare. The keys listed will be the attributes of the entity that the predicate is being built for. To select something more complex than a simple attribute, choose the Select Key item from the menu. This presents a key browser, shown in Figure 15 - 11. The browser shows the same attributes, but also includes the relationships defi ned for the entity. A Key Value path is like a directory path, in that it can specify an attribute of an entity related to an entity. For example, in the university data model, students are enrolled in a class. That class is taught by a faculty member, who belongs to a college. In a Class entity, the Key Value path instructor.department.name specifi es the name of the department of the faculty member that teaches the class. c15.indd 322c15.indd 322 1/21/10 3:52:11 PM1/21/10 3:52:11 PM Download at getcoolebook.com Data Modeling Interface ❘ 323 The type of the variable selected determines the operators and values that can be used in the expression. A Boolean variable presents only two operators (equals and does not equal) and two values (yes and no) to complete the expression. A string value presents numerous operators, all appropriate to matching string values. Select the desired operator from the operator menu. The selected operator further refi nes the type of value that will be used in the comparison. Some operators, such as the “ within ” operator, have more than one value. Normally, the value is a constant. Type the value of the constant into the fi eld. The text entered in the constant fi eld must agree with the type of data on the other side of the operator. You cannot compare a Decimal attribute with the string “ zero. ” The value can also be another attribute (specifi ed by a key) or a variable. Variables are values defi ned at run time and exist in an environment space associated with the predicate. Variables appear as $VAR_NAME in a predicate expression. To change the constant fi eld to a variable or key, Right/Control - click in the background of the expression and select Constant, Variable, or Key from the contextual menu, as shown in Figure 15 - 12. This is usually easiest to do by Right/Control - clicking just to the immediate right of the value fi eld. Some choices may be disabled, depending on the data type of the key or the operator selected. FIGURE 15-11 FIGURE 15-12 c15.indd 323c15.indd 323 1/21/10 3:52:11 PM1/21/10 3:52:11 PM Download at getcoolebook.com 324 ❘ CHAPTER 15 DATA MODELING To compare the key with a variable, enter the name of the variable in the Variable fi eld. Variable names cannot be verifi ed in the data model, so make sure they are spelled correctly. You select a key value just as you would a key on the left side of the expression. Compound Expressions You can combine simple expressions using logical operators to form compound expressions. Compound expressions are constructed by encompassing a simple expression, or expressions, within one of the logical operators: And, Or, or Not. In the cases of And and Or, the operator must encompass at least one other simple expression but can encompass more than two. The Not operator is unary and simply negates the expression it encloses. You have two ways of inserting logical operators into a predicate expression. The + button at the right of every simple expression inserts a new simple expression. If the expression is not already enclosed in a logical operator, a new logical operator is inserted ( AND, by default). If a logical operator already encloses the expression, that operator is expanded to include the new expression. The Add AND, Add OR, Add NOT, and Add Criteria commands are located on every key, logical operator, and Right/Control - click menu in the predicate builder. Selecting Add Criteria is identical to clicking a + button. The other three commands insert a new logical operator enclosing the expression. When you insert a new AND or OR operator, a new simple expression is also created and inserted below the existing expression. Remember that AND and OR operators must enclose at least two expressions. Add Criteria creates a logical operator only when it has to. The other three — Add AND, Add OR, and Add NOT — always insert a new logical operator. You can change a logical operator from AND or OR and back again using its menu. Drag expressions, even entire subexpression trees, to rearrange them in the tree. You can click and drag any portion of an expression ’ s background, but it ’ s often hard to miss the control areas of the pop-up menus. The most reliable drag point is the left end of the line that runs through the middle of the expression, or at the root of an expression tree. Figure 15 - 13 shows an OR expression being dragged to a different location in the expression tree. Dragging does not create new logical operators. However, if a logical operator contains only two expressions, dragging one of them to another subexpression deletes it — just as if you had deleted the expression. FIGURE 15-13 c15.indd 324c15.indd 324 1/21/10 3:52:12 PM1/21/10 3:52:12 PM Download at getcoolebook.com Data Modeling Interface ❘ 325 Use the – button to the right of the expression to delete it. You can delete expressions and NOT operators by choosing the Remove command from any of the expression menus. You cannot delete the logical operators AND and OR directly. To delete an AND or OR operator, delete or remove all but one of the expressions the operator encompasses. Textual Expressions Every expression constructed by the predicate builder has a textual representation as well. It is this textual version of the expression that you see in the data model browser. You can also enter predicate expressions directly, or you can insert textual subexpressions in the predicate builder. You may elect to do this because entering the expression directly is often easier than building one graphically, or because you want to use specialized functions or non - trivial Key Value paths. To enter an expression directly, type the formula into the Predicate fi eld, double - click the predicate expression in the fetched properties list, or add an Expression term in the predicate editor, as shown in Figure 15 - 14. The expression must be a valid predicate expression. As long as the predicate builder is open, the expression will be displayed just as you entered it. Be careful when pasting expressions that the predicate builder can ’ t represent. It is possible to enter expressions, or later change the defi nition of an entity, resulting in an expression that cannot be edited. If you fi nd yourself in this situation, copy the textual representation of the expression in the browser, delete the fetched property or request, and then create a new one by pasting the (modifi ed) expression into the Expression fi eld. FIGURE 15-15 FIGURE 15-14 When you close the predicate builder, the predicate is compiled and stored as archived Predicate objects in the data model. When you edit that predicate again, the predicate editor reinterprets the expression and creates a minimal representation of it. Consequently, the expression in the predicate builder may look different when you edit it again, but will logically represent the same statement. Figure 15 - 15 shows the expression previously shown in Figure 15 - 14, after it was saved and reopened. c15.indd 325c15.indd 325 1/21/10 3:52:12 PM1/21/10 3:52:12 PM Download at getcoolebook.com 326 ❘ CHAPTER 15 DATA MODELING CREATING AN INSTANT INTERFACE Xcode can create an “ instant interface ” from a data model. An instant interface produces a functional Cocoa user interface that allows you to enter and edit data in your data model. This can be a huge time saver if you are just getting your application going or just need a minimal interface in which to view or edit your data. You often have some portion of a working data model, but no data and little or nothing that resembles an application. To create an instant interface, you ’ ll fi rst need a window to put it in: open a nib document in Interface Builder that already has a Cocoa window in it, create a new nib document, or add a new window object to an existing nib document. You can now initiate the interface builder process from either Xcode or Interface Builder: To use Xcode, arrange the window so that it is visible on the screen alongside your data model window in Xcode. Switch back to the data model window. Select the Pointer tool. While holding down the Option key, click and drag an entity from the data model diagram and drop it into the Interface Builder window. When you start the drag, a shadow of the entity with a + sign follows the cursor. If it does not, you are not dragging a copy of the entity. To use Interface Builder, drag a Core Data Entity object (you ’ ll fi nd it in the Core Data group) from the library palette into the window. A browser appears. Select the project, data model, and entity for which you want to generate the interface. Xcode now asks you if you want an interface that represents one or many entity objects. Entry fi elds are created for each attribute. For a collection of entities, Fetch, Add, and Delete buttons can be created along with a table listing all of the instances in the collection. Figure 15 - 16 shows the instant interface created for many Faculty entities. ➤ ➤ FIGURE 15-16 c15.indd 326c15.indd 326 1/21/10 3:52:18 PM1/21/10 3:52:18 PM Download at getcoolebook.com . Data Modeling Interface ❘ 31 9 c15.indd 31 9c15.indd 31 9 1/21/10 3: 52:09 PM1/21/10 3: 52:09 PM Download at getcoolebook.com 32 0 ❘ CHAPTER 15 DATA MODELING application for professional photographers. fi eld. Some choices may be disabled, depending on the data type of the key or the operator selected. FIGURE 15-11 FIGURE 15-12 c15.indd 32 3c15.indd 32 3 1/21/10 3: 52:11 PM1/21/10 3: 52:11 PM Download. Value path instructor.department.name specifi es the name of the department of the faculty member that teaches the class. c15.indd 32 2c15.indd 32 2 1/21/10 3: 52:11 PM1/21/10 3: 52:11 PM Download at