TweetFollow Us on Twitter

Introduction to Core Data, Part IV

Volume Number: 22 (2006)
Issue Number: 2
Column Tag: Programming

Introduction to Core Data, Part IV

Storing Fetch Requests in your Data Model

by Jeff LaMarche

In the last Core Data article we talked about creating Fetch Requests programmatically using format strings and predicates. Using format strings to create Fetch Requests is a little bit inelegant, although it does give you a tremendous amount of flexibility. For your day-to-day, run-of-the-mill data fetching needs there is an easier way that gets those format strings out of your compiled code. You can create Fetch Requests right in your data model. This month, we're going to take a quick look at doing just that: We're going to build Fetch Requests and predicates right in Xcode and then look at how we get those Fetch Requests from our code.


In Xcode, I went ahead and built a simple data model with a single entity called Person that contains two string attributes called firstName and lastName, and a date attribute called birthDate. I also gave the entity a to-one relationship called spouse that can connect this entity to one other Person entity. I'll be using this simple data model to illustrate my points in the rest of this article. We've already discussed creating data models back in the first part of this series (Intro to Core Data, July, 2005), so I won't be guiding you through creating the data model this month. If you create a new project in Xcode using the Core Data Application project template, you should be able to quickly create the same data model yourself, or you can simply download my project from the MacTech FTP site. You might want to do that before you continue with the article, as I'll be making frequent references to this data model, however it is a simple enough data model that you can probably follow along without it.

Stored Fetch Requests

If you search Apple's documentation for information on how to store Fetch Requests in your data model, you might come to the conclusion that it simply can't be done. The ability to store Fetch Requests as part of the data model, although present in Core Data's predecessor Enterprise Objects Framework, seemed to be absent from the data model editor in Xcode when Core Data was first released. It was actually there, but it just wasn't very obvious; you had to change the view settings for the properties pane in order to see or work with Fetch Requests (Figure 1). The official documentation spends so little time on the topic that it's very easy to miss it.

Figure 1. Getting to the fetch requests stored in your data model.

With the recent release of Xcode 2.1, however, the eagle-eyed among you have probably noticed that a fourth option has been added to the property pane's add (plus sign) pop-up menu in the data model editor (Figure 2). Unlike with earlier releases, this option now shows up regardless of whether you've changed the pane to show Fetch Requests or not. That makes the functionality a little more obvious, and a little easier to access.

Figure 2. You can now add a fetch request when looking at the properties pane in any view mode.

If you decide to store fetch requests in your data model (and you should), you still have to know about predicates and format strings. All that information I gave you in the Core Data III article back in November was not for naught. Without a solid understanding of format strings and predicates, you wouldn't be able to use the functionality we're going to go over this month. See? There is a method to my madness.

The Predicate Builder

The way that we build Fetch Requests in Xcode is to use the Predicate Builder. This is a tool that is built into Xcode's data model editor, and is used for visually creating predicates (and the format string that they're based on). Why don't you go ahead and select the Person entity in your data model, then press the little plus sign in the properties pane (the top-middle pane). Select Add Fetch Request from the menu that pops up (Figure 2). You'll get a new Fetch Request, which you should go ahead and rename to olderThanSpouse in the top right pane. We are going to create a predicate that brings back all people who have a spouse and are older than them.

When you edit the name of this Fetch Request, you should see a button labeled Edit Predicate down below where you change the name. This button will call up the Predicate Builder. The large text field above the Edit Predicate button will show you the format-string associated with any predicate you build with the Predicate Builder. Although it looks like you should be able to edit the format string there, you actually can't, though you can select it and copy to the clipboard.

Figure 3. You can get to the predicate builder by pressing the Edit Predicate button when the fetch request is selected in the properties pane.

Go ahead and click that button to bring up the Predicate Builder. This interface probably looks familiar to you, at least if you've ever set up a rule in Mail, or defined a smart playlist in iTunes or a smart album in iPhoto. Predicates are built using a fairly standard-issue Apple rules interface (Figure 4). Almost. It's a very functional interface, but it's not as intuitive as the ones you've probably used. There's actually a little bit of hidden functionality in there, so let's take a closer look at it.

Figure 4. The Predicate Builder

Starting from the left side, the first thing you'll see is a pop-up menu. This is the key pop-up menu, sometimes simply referred to as the left hand side menu. This is where you define what you're comparing or, in other words, the left side of the format string. Most often, you're going to select one of the attributes of your entity. If you click the pop-up menu, you'll see that you have more options to select from than just the attributes of the current entity, however (Figure 5).

Figure 5. The key pop-up menu.

The top-most item in the menu is labeled Expression. You won't find this option well-documented anywhere unfortunately, but it's a very handy one nonetheless. This option changes the whole line to a single editable text field where you can plug in a format string. Why would you want to do that? Well, there might be times when you need to access a property using key-value coding that the data model editor doesn't know about, perhaps because you've created an attribute and left its type as undefined. Basically, if you know about an attribute that Xcode doesn't know about for any reason, you can use this option to manually enter the format string for one part of the predicate. In practice, you probably won't have to use this very often, but it's good to know it's there in case you ever need it.

The next option, labeled Select Key... brings up a browser to let you select an attribute. This might seem a little redundant, since the individual attributes of this entity are also available right in the pop-up menu, but you might have noticed that the spouse relationship was not accessible there, because it's not an attribute. Although this option can be used just to select an attribute of this entity, the primary reason it exists is so that you can drill down into other entities through this entity's relationships. Let's say we wanted to create a fetch request based on the birthday of the person's spouse. In that case, you could use the Select Key... option, click on the spouse entry in the left-most column, then click on the birthDate field in the second column (Figure 6). Don't actually do that, however. Click the Cancel button; I don't want you to actually select the spouse's birthDate field here.

Figure 6. Selecting attributes of a relationship using the Select Key... option.

The next few options are pretty self-explanatory; they are simply the attributes of this entity that you can select (firstName, lastName, birthDate) The options in the next section below the attributes allow you to add additional criteria to this rule. Here's where we start to see a deviation from the standard-issue Apple rules dialog. In most rules dialogs, such as iTunes' smart playlist dialog (Figure 7), you define a simple list of criteria and choose to require either all of the criteria, or any single criteria in the list.

Figure 7. The smart playlist rules dialog from iTunes.

The Predicate Builder allows you to create much more complex and robust rules than the rule dialogs in Apple's consumer applications. You can use all three of the standard Boolean operators (and, or, not), and choose a different operator for each pair of operations. You can also set precedence by creating a hierarchy of criteria.

The four Add options under the key pop-up menu all insert another criterion into the predicate, and function almost exactly the same. The only difference in the options, is which of the Boolean operators is used, although that can be changed after the fact. The bottom-most Add option is the same as pressing the plus button on the right-hand side of the row or the Add AND option. You can look at Figure 8 to get an idea of the sort of complex rules you can build using the Predicate Builder. The last option on the left-hand side menu simply removes the row and functions exactly like pressing the minus button on the right-hand side of the row.

Figure 8. Crafting a complex Fetch Request in the Predicate Builder.

As we move to the right, the next pop-up menu is the operator to be used for this row. The available operators change depending on the type of attribute selected in the key pop-up menu. Different options are presented, for example, when you select a string attribute than when you select a number or date attribute.

Last, but not least, the next field over is simply the value to be compared when evaluating this criterion. The example in Figure 8 shows a number of comparisons being made, but they're all being made to constant values typed right into this field. That's useful, but not always what you want. To be really useful, you'd need to be able to plug in values at runtime or, perhaps, compare one attribute to another. But there doesn't seem to be any way to enter anything other than a constant here, does there?

Well, here's a bit of hidden functionality. Go ahead and either right-click or control-click in the space to the left of the minus button that's used to delete the row. Lo and behold, you'll be presented with a contextual menu with some new options(Figure 9).

Figure 9. The hidden contextual menu.

The bottom five options are exactly the same as ones you saw in the left-hand key pop-up menu. They add or remove the current row. The top three options, however, are the magic options. They allow you to select the types values for the right-hand side of the equation. The default value, as you've seen, is Constant, which allows you to specify the comparison value right in the Predicate Builder.

Let's skip the second option (Variable) for just a second. We'll come back to it in a moment, but it's more involved than the other two, so let's look at the third option first. Go ahead and select Key. Doing that, will change the place where you used to be able to enter a constant value into another pop-up menu that looks identical to the left-hand side pop-up menu. This is the right-hand side key pop-up menu. This can be useful in many situations. We stated that we were going to create a Fetch Request to retrieve all people who are older than their spouse. Easy enough. Just choose Select Key... from the right-hand side pop-up menu and drill down to the spouse's birthDate as we did a moment ago on the left-hand side. Next, change the operator pop-up menu to greater-than and make sure birthDate is selected in the left-hand key pop-up menu. (Figure 10).

Figure 10. Comparing a key to another key

Go ahead and click the OK button; this Fetch Request is done. You can take a look in the Predicate: field to see the format string you just created. Now, let's create a little more complex Fetch Request. Go ahead and click the plus button in the lower left of the Fetch Request pane (the top-middle pane of the data model editor). When you added a Fetch Request before, the Property pane turned into the Fetch request pane, so this time, you can just click the plus button and don't have to select from a pop-up menu. When the new Fetch Request comes up, rename it to birthDateBetween, then click the Edit Predicate button to get back to the Predicate Builder. We're going to build a Fetch Request to retrieve all people with a birth date between two values, but we don't want to specify those two values until runtime.

Let's now talk about the second option on the contextual menu: Variable. If you control-click to the left of the minus button again and select that option, you'll get an editable text field labeled Variable (Figure 11). This is where the juju happens, baby. In this field, we can specify a variable name and later we'll be able to replace that variable with another value in our code. This is called a substitution variable or sometimes a bind variable. This is what makes stored Fetch Requests flexible enough to use for just about everything. If you specify a value here - which can be just about any string value you wish to use - you will then be able to substitute any other value of your choosing when you retrieve the Fetch Request from your data model.

If you're playing along at home, go ahead and change the operator pop-up menu from = to within, which will add a second row to this criteria. The new row is not a new criteria, but just a second parameter to the existing one. The within operator allows you to search based on a date attribute that falls between two other date values. Control-click to the right of the new row that got entered and change it also to variable. Now, go ahead and assign each of those variables a name. I chose FROMDATE and TODATE, as you can see from Figure 11. Once you've got it set up, click the OK button.

Figure 11. Specifying a substitution variable in Predicate Builder.

After the Predicate Builder is dismissed, go ahead and look at that large text field labeled Predicate:. That will show you the format string that will be used for creating this Fetch Request. Notice the dollar sign in front of FROMDATE and THRUDATE? That's how substitution variables are specified in format strings.

Go ahead and save your data model and let's take a look at how we can pull our Fetch Request out of the data model instead of having to create them from scratch, as we did in the previous article.

Getting Fetch Requests Out Of the Data Model

Technically speaking, you aren't storing a Fetch Request in your data model, even though it's usually referred to that way. Your data model is not like a nib file, where serialized objects are being stored. Instead, what's really happening is you are creating a template from which a new Fetch Request can be instantiate at run time. This is a relatively straight forward process.

One thing that might trip you up is the fact that you don't use NSMangedObjectContext to retrieve your Fetch Requests. If you've been using Core Data for a little while, you've probably gotten accustomed to using the context for retrieving, inserting, and editing your data. A Fetch Request, however, is not data. It's part of your data model. Therefore, instead of using the context, you have to use your managed object model (NSManagedObjectModel) to retrieve your Fetch Requests. Fortunately, the managed object model is available through an accessor method that gets created automatically for you when you use one of the Core Data project templates. In a Core Data Application project, you can get the managed object model using the managedObjectModel method of the application delegate class (applicationName_AppDelegate). In a Core Data document-based application, you use the same method, but call it on your document class instead (which inherits the method from NSPersistentDocument).

Once you have a reference to your managed object model, it's simply a matter of asking it for your Fetch Request by name. If you aren't using substitution variables, it's very easy. Here's how we would get that first Fetch Request we created:

Getting a Fetch Request from your data model

NSManagedObjectModel *model = [self managedObjectModel];
NSFetchRequest *fetch = 
   [model fetchRequestTemplateForName:@"olderThanSpouse"];

That's all there is to that one. After this call, you're ready to fetch data from the context.

On the other hand, if you have specified substitution variables in your Fetch Request, then you have to build a dictionary containing the substitution variable names as the key values, and the values you wish to replace them with as the corresponding objects. In our case, that means creating an NSDictionary with two entries. One with a key value of @"FROMDATE" and another with a key value of @"TODATE". The objects to be passed for both of these keys need to be NSDate object instances since we are comparing with a date field. If we were comparing a numeric field, we'd use NSNumber, and if were comparing a string field, we would use an NSString. Here's an example of getting our second Fetch Request out of the data model:

Getting a Fetch Request with substitution variables

NSManagedObjectModel *model = [self managedObjectModel];
NSDate *fromDate = [NSCalendarDate 
NSDate *toDate = [NSCalendarDate 
NSDictionary *subs = [NSDictionary 
   dictionaryWithObjectsAndKeys:fromDate, @"FROMDATE", 
   toDate, @"TODATE", nil];
NSFetchRequest *fetch = 
   [model fetchRequestFromTemplateWithName:
   @"birthDateBetween" substitutionVariables:subs];

Once either of these two chunks of code fires, you will have a pointer to a Fetch Request. It is exactly the same as if you had created it using a format string and a predicate like we did in the previous article, but with less code and without having the format string inside your compiled application.


Storing the definition of your Fetch Requests inside your data model has many advantages. You can use the Predicate Builder to more easily build complex criteria it allows you to move potentially complex format strings out of your compiled code and into your data model. It also allows you to use bind variables to let you alter your Fetch Requests at runtime based on user-provided data.

These tools provide you with the ability to create and use just about any type of Fetch Request you might need right in your data model, and there is very little reason in most situation not to use it. Unfortunately, the fact that the functionality was sort of squirreled away where it wasn't obvious means it is probably being underused, but now that you where it is and how to use it, you can go ahead and get all those yucky format strings and predicates out of your code.

Jeff LaMarche wrote his first line of code in Applesoft Basic on a Bell & Howell "Darth Vader" Apple ][+ in 1980 and he's owned at least one Apple computer at all times since. In addition to writing, Jeff codes in a variety of languages, with Cocoa Objective-C being, by far, his favorite. Feel free to drop him a line anytime at

Apple Inc.
Microsoft Corpora
Google Inc.

MacTech Search:
Community Search:

Software Updates via MacUpdate

Ember 1.8 - Versatile digital scrapbook....
Ember (formerly LittleSnapper) is your digital scrapbook of things that inspire you: websites, photos, apps or other things. Just drag in images that you want to keep, organize them into relevant... Read more
OmniPlan 2.3.6 - Robust project manageme...
With OmniPlan, you can create logical, manageable project plans with Gantt charts, schedules, summaries, milestones, and critical paths. Break down the tasks needed to make your project a success,... Read more
Command-C 1.1.1 - Clipboard sharing tool...
Command-C is a revolutionary app which makes easy to share your clipboard between iOS and OS X using your local WiFi network, even if the app is not currently opened. Copy anything (text, pictures,... Read more
Knock 1.1.7 - Unlock your Mac by knockin...
Knock is a faster, safer way to sign in. You keep your iPhone with you all the time. Now you can use it as a password. You never have to open the app -- just knock on your phone twice, even when it's... Read more
Mellel 3.3.6 - Powerful word processor w...
Mellel is the leading word processor for OS X and has been widely considered the industry standard since its inception. Mellel focuses on writers and scholars for technical writing and multilingual... Read more
LibreOffice - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
Freeway Pro 7.0 - Drag-and-drop Web desi...
Freeway Pro lets you build websites with speed and precision... without writing a line of code! With it's user-oriented drag-and-drop interface, Freeway Pro helps you piece together the website of... Read more
Drive Genius 3.2.4 - Powerful system uti...
Drive Genius is an OS X utility designed to provide unsurpassed storage management. Featuring an easy-to-use interface, Drive Genius is packed with powerful tools such as a drive optimizer, a... Read more
Vitamin-R 2.15 - Personal productivity t...
Vitamin-R creates the optimal conditions for your brain to work at its best by structuring your work into short bursts of distraction-free, highly focused activity alternating with opportunities for... Read more
Toast Titanium 12.0 - The ultimate media...
Toast Titanium goes way beyond the very basic burning in the Mac OS and iLife software, and sets the standard for burning CDs, DVDs, and now Blu-ray discs on the Mac. Create superior sounding audio... Read more

Latest Forum Discussions

See All

Dawn of the Immortals Review
Dawn of the Immortals Review By Jennifer Allen on July 31st, 2014 Our Rating: :: RESPECTABLE EXPLORATIONUniversal App - Designed for iPhone and iPad Dawn of the Immortals might not re-invent the wheel, but it does tweak it a little... | Read more »
80 Days Review
80 Days Review By Jennifer Allen on July 31st, 2014 Our Rating: :: EPIC ADVENTUREUniversal App - Designed for iPhone and iPad A fantastic and fascinating re-envisioning of the classic novel by Jules Verne, 80 Days is a delightful... | Read more »
Battleheart Legacy Guide
The world of Battleheart Legacy is fun and deep; full of wizards, warriors, and witches. Here are some tips and tactics to help you get the most enjoyment out of this great game. | Read more »
Puzzle Roo Review
Puzzle Roo Review By Jennifer Allen on July 31st, 2014 Our Rating: :: PUZZLE-BASED TWISTUniversal App - Designed for iPhone and iPad A different take on the usual block dropping puzzle game, Puzzle Roo is quite pleasant.   | Read more »
Super Crossfire Re-Release Super Crossfi...
Super Crossfire Re-Release Super Crossfighter Coming Soon, Other Radiangames Titles Go 50% Off Posted by Ellis Spice on July 31st, 2014 [ | Read more »
Hexiled Review
Hexiled Review By Rob Thomas on July 31st, 2014 Our Rating: :: HEX SELLSUniversal App - Designed for iPhone and iPad In space, no one can hear you… spell? Hexiled is a neat concept for a word scramble puzzle, but it doesn’t go too... | Read more »
Summoners War: Sky Arena Passes 10 Milli...
Summoners War: Sky Arena Passes 10 Million Installs! Posted by Jessica Fisher on July 31st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Deep Loot Review
Deep Loot Review By Jennifer Allen on July 31st, 2014 Our Rating: :: DIVE DEEPUniversal App - Designed for iPhone and iPad Dive deep in this fun explore-em-up that’s a little grind heavy but ultimately quite entertaining.   | Read more »
Despicable Me: Minion Rush is One Year O...
Despicable Me: Minion Rush is One Year Old, Gets its Biggest Update Yet Posted by Jennifer Allen on July 31st, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Fish & Shark Review
Fish & Shark Review By Jordan Minor on July 31st, 2014 Our Rating: :: FLAPPY FISHUniversal App - Designed for iPhone and iPad Fish & Shark’s beauty is only scale deep.   | Read more »

Price Scanner via

Save up to $130 on an iPad mini with Apple re...
The Apple Store has Certified Refurbished 2nd generation iPad minis with Retina Displays available for up to $130 off the cost of new models, starting at $339. Apple’s one-year warranty is included... Read more
iPad Cannibalization Threat “Overblown”
Seeking Alpha’s Kevin Greenhalgh observes that while many commentators think Apple’s forthcoming 5.5-inch panel iPhone 6 will cannibalize iPad sales, in his estimation, these concerns are being... Read more
Primate Labs Releases July 2014 MacBook Pro P...
Primate Labs’ John Poole has posted Geekbench 3 results for most of the new MacBook Pro models that Apple released on Tuesday. Poole observes that overall performance improvements for the new MacBook... Read more
Apple Re-Releases Bugfixed MacBook Air EFI Fi...
Apple has posted a bugfixed version EFI Firmware Update 2.9 a for MacBook Air (Mid 2011) models. The update addresses an issue where systems may take longer to wake from sleep than expected, and... Read more
Save $50 on the 2.5GHz Mac mini, plus free sh...
B&H Photo has the 2.5GHz Mac mini on sale for $549.99 including free shipping. That’s $50 off MSRP, and B&H will also include a free copy of Parallels Desktop software. NY sales tax only. Read more
Save up to $140 on an iPad Air with Apple ref...
Apple is offering Certified Refurbished iPad Airs for up to $140 off MSRP. Apple’s one-year warranty is included with each model, and shipping is free. Stock tends to come and go with some of these... Read more
$250 price drop on leftover 15-inch Retina Ma...
B&H Photo has dropped prices on 2013 15″ Retina MacBook Pros by $250 off original MSRP. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.3GHz Retina MacBook Pro: $2249, $250 off... Read more
More iPad Upgrade Musings – The ‘Book Mystiqu...
Much discussed recently, what with Apple reporting iPad sales shrinkage over two consecutive quarters, is that it had apparently been widely assumed that tablet users would follow a two-year hardware... Read more
13-inch 2.5GHz MacBook Pro on sale for $999,...
Best Buy has the 13″ 2.5GHz MacBook Pro available for $999.99 on their online store. Choose free shipping or free instant local store pickup (if available). Their price is $100 off MSRP. Price is... Read more
Save up to $300 on an iMac with Apple refurbi...
The Apple Store has Apple Certified Refurbished iMacs available for up to $300 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free. These are the best prices on... Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Sr. Product Leader, *Apple* Store Apps - Ap...
**Job Summary** Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
Sr Software Lead Engineer, *Apple* Online S...
Sr Software Lead Engineer, Apple Online Store Publishing Systems Keywords: Company: Apple Job Code: E3PCAK8MgYYkw Location (City or ZIP): Santa Clara Status: Full Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.