Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I apply multiple predicates to an NSFetchRequest? Would it be better to manually parse my results?

Ok I have a basic iPad app that asks for 5 search/filter criteria from the user. Based on this data, I need to go to my core data db, and pull out any managed objects that fit that criteria. It seems like I need to apply more than one predicate to the same request, is that possible? Or could I just write a really long fancy predicate? With multiple requirements? How should I approach that?

Would it be a good idea to just grab all the entities through the fetch request, and then loop through each array and grab any objects that I find that fit my search criteria?

Please advise!

like image 356
Lizza Avatar asked Nov 14 '11 23:11

Lizza


1 Answers

Yes it's possible. You're looking for compound predicates and here's an example with AND predicates:

NSPredicate *compoundPredicate     = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray of Predicates]]; 

You can also use notPredicateWithSubpredicates and orPredicateWithSubpredicates depending on your needs.

Link to documentation https://developer.apple.com/documentation/foundation/nscompoundpredicate

like image 66
Rog Avatar answered Sep 19 '22 06:09

Rog