CoreData fetch request through an abstract managed entity for a specific managed entity

I am trying to execute a query on the context of a managed object with a predicate that checks the key path that exists in some subclasses of the abstract class.

For example, here is a part of the object model

Library::NSManagedObject - AllMovies::to-many relationship->Movie Movie::NSManagedObject (abstract) - type::String - name::String - mylibrary::to-one relationship->Library HorrorMovie::Movie - monster::String - ghosts::BOOL RomanceMovie::Movie - percociouskid::String - hasferret::BOOL 

If I configured the following query to select

 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Library" inManagedObjectContext:moc]; NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; [request setEntity:entityDescription]; NSPredicate *predicate = [NSPredicate predicateWithFormat: @"(SUBQUERY(AllMovies, $movies, $movies.type like[c] 'horror' and $movies.moster like[c] 'yeti' ) .@count != 0)" ] [request setPredicate:predicate]; NSArray *array = [moc executeFetchRequest:request error:&error]; 

Fetching a query returns an error, for example

 keypath $movies.monster not found in entity <NSSQLEntity Movie id=2> 

There doesn't seem to be a way to make a lazy predicate estimate. Some of the other things that I have already tried are ANY predicate using the CAST keyword, trying to replace the β€œAllMovies” in SUBQUERY with another SUBQUERY to return a group of objects that matches the type value.

It would be possible to fulfill several requests for each qualification type, but this is rude, slow and cumbersome.

This is under OS X 10.6 with persistent SQL storage. Running in a memory store is not an option since I work with 1 million + Libraries (the project really has nothing to do with movies, but I thought it was a good example).

Thanks Rob

+4
source share
1 answer

This will not work because the model of your object simply tells Core Data that the AllMovies relation contains Movie objects. Therefore, Core Data expects that it can only send messages that the Movie object understands. When it initiates a selection, it checks the predicate for each individual Movie object (as determined by the nature of the selection).

However, neither the Movie object nor the RomanceMovie object understands the monster message (because they lack an attribute.) The predicate test is meaningless. That is why you get an error.

You need to rethink your design. Using object inheritance may not be the way you want to handle it.

+1
source

All Articles