Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate vs. NSCompoundPredicate?

What are the pros and cons for using a single NSPredicate with a value like "Condition1 AND condition2" vs. using a NSCompoundPredicate with two subpredicates?

Even if I construct it dynamically, the first option seems much more compact to code.

like image 914
Amiram Stark Avatar asked Oct 21 '11 10:10

Amiram Stark


1 Answers

You've almost answered your own question - it's more a cleanliness of code question than a performance question!

NSCompoundPredicate is more useful if you are creating lots of conditions in seperate parts of your code that you then want to combine.

If your predicate is being created all in one place, just use ' AND ' in a format string.

It's really up to you when you decide that your predicate is complicated enough that creating it needs to be split into separate methods.

i.e.

If it's just a AND b then use a single predicate

If it's a AND (b OR c OR c) AND (e OR b) then use compound predicates!

like image 136
deanWombourne Avatar answered Sep 26 '22 19:09

deanWombourne