5

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.

Community
  • 1
  • 1
Darren
  • 10,091
  • 18
  • 65
  • 108
  • The class in question is not an NSObject so it doesn't respond to "class" or "className" – Stavash May 19 '13 at 15:39
  • Company and MyCompany are NSObjects as as they inherit from NSManagedObject, but I realize now that the predicate is being applied to the NSSQLEntity and so my situation is not equivalent to the situation those answers were responding to. However I still need a solution to my problem. – Darren May 19 '13 at 15:49

1 Answers1

10

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382