Core Data NSPredicate filter by entity class?

2019-06-17 09:40发布

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.

2条回答
唯我独甜
2楼-- · 2019-06-17 10:12

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".

查看更多
狗以群分
3楼-- · 2019-06-17 10:21

If you have a required field in Contact, you can filter on that field being not null. If you don't have a required field in Contact (that is not in Person), you could add a dummy required field and filter on that field.

查看更多
登录 后发表回答