Search code examples
objective-cnspredicateone-to-manyrelationships

Core Data- NSPredicate and to-many relationship


I have a one to many relationship between Games and Players. There is a relationship on Games to Players called players.

Games <-->> Players

The Games entity has a relationship called players. Players has an attributed named 'order'

I am unable to use a predicate to query on the relationship. Here is the code I am using:

    -(NSArray *)returnPlayerLastAtBat: (int)rosterNo
    {
        NSError *error;

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription 
                                       entityForName:@"Games" inManagedObjectContext:managedObjectContext];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                  @"(finished = '%d') and (players.order = '%d')",0,rosterNo];
        [fetchRequest setEntity:entity];
        [fetchRequest setPredicate:predicate];

        NSArray* result = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
        [fetchRequest release];

return result;
}

Not sure what I am doing wrong- I am able to work with predicated throughout my project- but this one has been stuck.


Solution

  • Take a look at this SO question. And as the mvds mentionned, remove the quotes around the %d.