Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you reference child entity name in a predicate for a fetch request of the parent entity?

I have the need to create a complex predicate for an abstract base object. I want to have separate predicate queries for different inheriting entities and key off the sub-entity type, the example below is what I would like to do, however, I have not been able to find a way to reference the entity name or type in the predicate.

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"MyCommonObjectBase" inManagedObjectContext:myContext];

NSPredicate *subclassAPredicate = [NSPredicate predicateWithFormat:@"someValue > %@ && entityName = %@", 100, @"SubclassA"];
NSPredicate *subclassBPredicate = [NSPredicate predicateWithFormat:@"someValue < %@ && entityName = %@", 50, @"SubclassB"];

request.predicate = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:subclassAPredicate, subclassBPredicate, nil]];
like image 767
Greg Martin Avatar asked Oct 16 '09 17:10

Greg Martin


1 Answers

I got a response from Apple at http://bugreport.apple.com problem number 7318897:

entity.name is not a modeled property, so it's not legal. it works by accident on the binary store. The correct way to fetch subentities is to do so on the NSFetchRequest and use either setIncludesSubentities or not.

So it seems the proper solution is to have separate fetch requests and to merge the result sets after execution.

like image 54
Greg Martin Avatar answered Nov 16 '22 01:11

Greg Martin