Search code examples
objective-ccore-datanspredicateformat-string

Core Data Predicate "Unable to parse the format string"


I'm trying to fetch a set of NSManagedObjects that have specific relationships to other objects.

So I have 4 managed objects: UserPick, Game, Group, User. The UserPick has to-one relationship to Game, Group, User individually. I need to find all UserPicks that have a specific Game, Group and User, but I cannot seem to structure the predicate correctly:

I've tried:

[NSPredicate predicateWithFormat:@"%K = %@, %K = %@, %K = %@",
UserPickRelationships.game, game,
UserPickRelationships.group, group,
UserPickRelationships.user, user]

and:

[NSPredicate predicateWithFormat:@"%K = %@, %K = %@, %K = %@",
UserPickRelationships.game, game.objectID,
UserPickRelationships.group, group.objectID,
UserPickRelationships.user, user.objectID]

finally:

[NSPredicate predicateWithFormat:@"%K.%K = %@, %K.%K = %@, %K.%K = %@",
UserPickRelationships.game, @"objectID", game.objectID,
UserPickRelationships.group, @"objectID", group.objectID,
UserPickRelationships.user, @"objectID", user.objectID]

I've tried using %@ and %K in every conceivable combination. All I ever get back is: NSPredicate cannot parse format string. How can I do this?


Solution

  • You probably want to combine the conditions with "AND":

    [NSPredicate predicateWithFormat:@"%K = %@ AND %K = %@ AND %K = %@",
    UserPickRelationships.game, game,
    UserPickRelationships.group, group,
    UserPickRelationships.user, user]
    

    (assuming that UserPickRelationships.game evaluates to the string "game").