Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data NSPredicate filter by entity class?

How would I create an NSPredicate to filter by entity of class Contact?

The solution to NSPredicate check for kind of object class crashes:

[NSPredicate predicateWithFormat:@"person.class == %@", [Contact class]];

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
    reason: 'keypath person.class not found in entity <NSSQLEntity User id=12>'

Background

I have the Core Data entities User (app user) & Contact (address book contact), both children of Person (abstract entity). Person has many identities, which can be emails, phones, or usernames.

To create a text-message-like typeahead, I want to create an NSFetchRequest for Identity entities where person is of class Contact.

like image 675
ma11hew28 Avatar asked Apr 02 '12 22:04

ma11hew28


1 Answers

You are perhaps mixing up a few things. The answer you indicate is about a predicate to filter an array of UIViews. A predicate in core data works in a slightly different way.

To achieve your desired result, simply set the entity of your fetch request to @"Contact".

like image 89
Mundi Avatar answered Oct 22 '22 13:10

Mundi