Skip navigation.
 
ml[Leopard] SUBQUERY predicate with CoreData
FROM : Barry Wark
DATE : Mon Feb 04 23:02:57 2008

I have an example CoreData managed object model that contains an
entity Foo{fooProp:string; keyValues:{to- m,eany relationship to
KeyValue}) and an entity KeyValue(key:string,value:float; foo:{to-one
relationship to Foo}). I'm having trouble executing a fetch request
with a predicate that contains a SUBQUERY expression on a managed
object context using this model. I've included the .xcdatamodel (zip'd
with an added extension -- hopefully it will make it pass the list
filter). In the Xcode "CoreData application", using the included
.xcdatamodel and adding this method to the app delegate:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    // add some examples to the XML store
    id foo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo"

inManagedObjectContext:[self managedObjectContext]];
    [foo setFooProp:@"fooProp1"];

    id kv = [NSEntityDescription insertNewObjectForEntityForName:@"KeyValue"
                                          inManagedObjectContext:[self
managedObjectContext]];

    [kv setKey:@"key1"];
    [kv setValue:[NSNumber numberWithFloat:1.0] forKey:@"floatValue"];

    [foo addKeyValuesObject:kv];

    foo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo"

inManagedObjectContext:[self managedObjectContext]];
    [foo setFooProp:@"fooProp2"];

    kv = [NSEntityDescription insertNewObjectForEntityForName:@"KeyValue"
                                          inManagedObjectContext:[self
managedObjectContext]];

    [kv setKey:@"key2"];
    [kv setValue:[NSNumber numberWithFloat:2.0] forKey:@"floatValue"];

    [foo addKeyValuesObject:kv];


    NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease];
    [fetch setEntity:[NSEntityDescription entityForName:@"Foo"
                                inManagedObjectContext:[self
managedObjectContext]]];

    [fetch setPredicate:[NSPredicate
predicateWithFormat:@"(SUBQUERY(self.keyValues, $kv, $kv.key like
'key1' && $kv.floatValue==1.0).count > 0) && fooProp like
'fooProp1'"]];

    id result = [[self managedObjectContext] executeFetchRequest:fetch

error:nil]; //app crashes here

    NSLog(@"%@", result);

}

produces the following (relevant) output at the console

*** -[NSCFArray compare:]: unrecognized selector sent to instance 0x195220

and the application crashes during the  [[self managedObjectContext]
executeFetchRequest:fetch error:nil] method.

I know the documentation on using SUBQUERY is still on the way, but I
was hoping that one of the CoreData gurus could suggest what I'm doing
wrong.

Thanks!

Barry

Related mailsAuthorDate
ml[Leopard] SUBQUERY predicate with CoreData Barry Wark Feb 4, 23:02
mlRe: [Leopard] SUBQUERY predicate with CoreData Melissa J. Turner Feb 4, 23:35
mlre: [Leopard] SUBQUERY predicate with CoreData Ben Trumbull Feb 5, 00:29
mlRe: [Leopard] SUBQUERY predicate with CoreData Barry Wark Feb 5, 02:00
mlRe: [Leopard] SUBQUERY predicate with CoreData I. Savant Feb 5, 02:03