Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

The Predicate Builder

You use the predicate builder to create predicates for fetched properties and for fetch request templates. For more about predicates, see NSPredicate and for more about fetched properties, see NSFetchedPropertyDescription. Fetch request templates allow you to create predefined instances of NSFetchRequest that are stored in the model. You can either define all aspects of a fetch, or you can allow for runtime substitution of values for given variables. Fetch templates are associated with the entity against which the fetch will be made, that is, instances of which the fetch will return. For more about fetch templates, see NSManagedObjectModel.

With the predicate builder, you can build predicates of arbitrary complexity. The initial display shows a simple comparison predicate. The left-hand side is pop-up menu that allows you to choose the key used in a key path expression; the right-hand side is a text field that allows you to specify a constant value; and between them is a pop-up menu that allows you to choose a comparison operator.


Figure 12-9  Predicate builder

Figure 12-9 Predicate builder

As with the rest of the modeling tool, the predicate builder simply provides a graphical means of defining a collection of objects that you could otherwise create programmatically. The code equivalent of the predicate revenue >= 100000000 is as follows.

NSExpression *lhs = [NSExpression expressionForKeyPath:@"revenue"];
NSExpression *rhs = NSExpression *rhs = [NSExpression expressionForConstantValue:[NSDecimalNumber  numberWithInt:100000000]];
NSComparisonPredicate  *predicate = [NSComparisonPredicate
    predicateWithLeftExpression:lhs
    rightExpression:rhs
    modifier:NSDirectPredicateModifier
    type:NSGreaterThanOrEqualToPredicateOperatorType
    options:0];

In this section:

Right-Hand Side
Left-Hand Side
Compound Predicates


Right-Hand Side

In addition to a constant value, you can also define the right-hand side of a comparison predicate to be a variable or another key. This is necessary if you are creating either fetch request templates that require substitution variables or defining fetched properties and need to use the $FETCH_SOURCE variable in the predicate.

You change the type of the right hand side expression using the contextual menu shown in Figure 12-10 (you must Control-click “empty space” in the line of the criteria—for example, at the end or between the pop-up menus). This changes the constant value field into a variable field or a key pop-up menu as appropriate.


Figure 12-10  Right-hand side expression type

Figure 12-10 Right-hand side expression type

Left-Hand Side

The key pop-up menu, shown in Figure 12-11, displays only attributes of the entity with which the predicate is associated. To use a key path (that is, to follow relationships), choose the Select Key item in the key pop-up menu. This displays a browser, shown in Figure 12-12, from which you can choose the key or key path you want.


Figure 12-11  Predicate keys

Figure 12-11 Predicate keys


Figure 12-12  Adding a key path

Figure 12-12 Adding a key path

Compound Predicates

You can add logical operators (AND, NOT, and OR) to create compound predicates of arbitrary depth and complexity. To add a specific logical operator, use the contextual menu, shown in Figure 12-13.


Figure 12-13  Creating a compound predicate

Figure 12-13 Creating a compound predicate

You can also add peer predicates by clicking the round button with the plus sign, or using the Add Criteria command in the contextual menu—these add an AND operator. You can change a logical operator using the pop-up menu. You can rearrage the predicate hierarchy by dragging. To remove a predicate, click the round button with the minus sign, or use the contextual menu. The predicate builder will try to rebuild the remaining predicate as it can, removing comparison operators where appropriate.



< Previous PageNext Page > Hide TOC


Last updated: 2006-11-07




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice