Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate that filters out subclass results

I have two classes, Company and MyCompany. MyCompany is a subclass of Company, and Company is a subclass of NSManagedObject. I am trying to write a predicate for an NSFetchRequest that will return results of the class Company, but filter out MyCompany objects.

I have tried the following (suggested from here https://stackoverflow.com/a/8065935/472344):

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"class != %@",NSStringFromClass([myCompany class])];

But I get an error:

'keypath class not found in entity <NSSQLEntity CKCompany id=1>'

I also tried (suggested from here https://stackoverflow.com/a/11693983/472344, I know I really want not SELF isKindOfClass, but I was just testing with the exact same command as given in the answer):

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self isKindOfClass: %@", [myCompany class];

And got the following error:

'Unknown/unsupported comparison predicate operator type'

How can I write a predicate to achieve what I want? I am supporting iOS 5 and above.

like image 550
Darren Avatar asked May 19 '13 15:05

Darren


1 Answers

You can set

[fetchRequest setIncludesSubentities:NO];

so that the fetch request returns only objects of exactly the entity type of the request, and does not include subentities.

It is (as far as I know) not possible to refer to the objects class or entity name in a predicate if the predicate is used in a Core Data fetch request.

like image 62
Martin R Avatar answered Sep 20 '22 19:09

Martin R