Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Friday, September 27, 2013

Compound Predicates (Multiple Where Clause Params) in iOS Core Data

So, you want to select an entity row from an iOS Core Data table using multiple where-clause-like parameters, eh?

"Predicates" (don't you love it when languages and platforms abstract their terminology away to the extreme?)... predicates are basically parameter equivalents in a WHERE clause.

select * from users where is_profitable_customer = 1

That statement in yellow there, that's a "predicate" {to be said in a snooty tone}. And this:
select * from users where is_profitable_customer = 1 AND calls_me_at_3am = 0

...well, that's what is referred to as a "compound predicate".

Ok, enough of this... lets see some code. If I want to select from a Core Data table with multiple where-clause parameters, I do this:

// Dim up a fetch request for our entity: UserModel, 
// which lets just assume exists
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"UserModel"];

// Our first predicate is gonna help us find the 
// customers that make us money
NSPredicate *profitableCustomerPredicate = [NSPredicate predicateWithFormat:@"is_profitable_customer = %d", 1];

// Our second predicate is gonna filter out the customers 
// that make life difficult.
NSPredicate *annoyingCustomerFilterPredicate = [NSPredicate predicateWithFormat:@"calls_me_at_3am = %d", 0];
// And we're going to make "Predicate Soup" combining 
// them into one WHERE statement
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubPredicates:@[profitableCustomerPredicate, annoyingCustomerFilterPredicate]];
// And assign this NSCompoundPredicate (which is just 
// an NSPredicate after all) to the request
request.predicate = compoundPredicate;

// Sigh... sort descriptors, Core Data won't function 
// without them. I call that a FLAW in Core Data!
NSSortDescriptor *sortDescr = [NSSortDescriptor sortDescriptorWithKey:@"is_profitable_customer" ascending:YES];

request.sortDescriptors = [NSArray arrayWithObject:sortDescr];

// Lets go fishing! Fetch that data... gives us back 
// an array of our Entities.
NSArray *matches = [context executeFetchRequest:request error:&error];

And there you have it. 

BEST PRACTICE ALERT! Of course, you can use single predicates, but why not just always use compound ones so they're already coded up the way you need them when you're ready to add more statements to your where clause. Do yourself this favor at least.

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates