0
votes

I have two Entities one called Games and one called Teams. The Games entity has a to one relationship to Teams called teams and the Teams entity has a to many relationship to Games called games. (A team can be in many games but a game can only have 1 team. I am using a separate entity for Opponents)

I am selecting a team by using it's ID. Here is my code for adding a team to the Games entity:

Games *newGame = (Games *) [NSEntityDescription insertNewObjectForEntityForName:@"Games" inManagedObjectContext:self.managedObjectContext];

    NSFetchRequest *fetchTeams = [[NSFetchRequest alloc] init];
    NSEntityDescription *fetchedTeam = [NSEntityDescription entityForName:@"Teams"
                                                   inManagedObjectContext:self.managedObjectContext];
    [fetchTeams setEntity:fetchedTeam];
    NSArray *fetchedTeams = [self.managedObjectContext executeFetchRequest:fetchTeams error:&error];

    for (Teams *myTeam in fetchedTeams) {

        if (myTeam.teamID == teamid){

            newGame.teams = myTeam;

        }
    }

The error I am getting is: 'NSInvalidArgumentException', reason: 'The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet.'

I don't understand it, newGame.teams is an object of Teams , it is not an NSSet. If I was doing Teams.games it would be an NSSet.

What am I doing wrong?

1
Are you sure this is where it's crashing? I've seen such error but only when executing fetch requests with predicates. - lammert

1 Answers

0
votes

You've not described what is the data type of variable "teamid" here. Hence, I'm assuming that it must be some primitive type int.

Based on this assumption, you can make the following changes in your code:

if(fetchedTeams!=nil)
{
  if(fetchedTeams.count>0)
  {
     for(Teams *myTeam in fetchedTeams)
     {
       //check here because coredata stores a number in NSNumber object. 
       //Hence you've to get the intValue to make a equality check, like below

       if(myTeam.teamID.intValue == teamid) 
       {
          //do your stuff here.
          //Also check the last part of my answer, I have a question here.
       }

     }
  }
}

In your code you have "newGame.teams = myTeam". What is the data type of "teams" in "newGame" ? is it Teams* or NSSet* ?