small medium large xlarge

Back to: All Forums  Core Data
Generic-user-small
24 Aug 2009, 01:18
whitman stone (19 posts)

I have a Product managed object, and it has a one-to-many relationship with a Department managed object.

product 1  --- dept 1
               dept 3
               dept 4

departments.departmentId is or should be the KVC path to it. departments is an NSSet property on the Product MO, (and a relationship-with-an-inverse in the object model) and departmentId is a string.

Have I got my predicate’s KVC syntax wrong somehow?

        NSEntityDescription *entityDescription = [NSEntityDescription
                                                  entityForName:@"Product" inManagedObjectContext:moc]; 
        [fetchRequest setEntity:entityDescription];
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                  @"departments.departmentId == '100011'"]; 
        [ fetchRequest setPredicate:predicate];
        

        NSArray *array = [self.managedObjectContext executeFetchRequest: fetchRequest error:&_cd_error]; 

exception triggered by fetchRequest here “to-many relationship not allowed here” NSException

Can anyone tell me what is going on here? Fast? :-)

Whit

Generic-user-small
24 Aug 2009, 01:55
whitman stone (19 posts)

answer: wrong syntax. This is right, though pretty freakin far from intuitive–looks like they want a subquery syntax internally rather than a join, so it parses out to handle collections too. whatever.

        NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                  @"ANY departments.departmentId in %@", [NSArray arrayWithObject:@"100002"] ];
        [request setPredicate:predicate];
 
Avatarsmall_pragsmall
25 Aug 2009, 16:32
Marcus S. Zarra (284 posts)

the reason for that is because @departments.departmentId@ actually will return back an array of objects rather than a single object to compare. Therefore you need to use the @in@ test instead of equality as you figured out.

You must be logged in to comment