title: How to create a Core Data relationship with two existing objects
body: <p>I've created a many-to-many relationship between two entities: event&lt;&lt;-->>team. 
  Selecting an event brings you to a detail page where you can change settings/associate teams that will partake in the event. The team page presents the user with a list of teams (created earlier in the process) that can be selected (with a checkmark) to associate to the event. The event MOC is passed to the team view which has its own MOC for team. When you select the team(s) that will participate in the event, I'm having trouble creating the relationship to the event. In the save method: </p>
  
  <pre><code>-(void)add 
  {
  for (int i = 0; i &lt; dataArray.count; ++i) 
  {
      NSDictionary *item = [dataArray objectAtIndex:i];
  
      NSString *name = [item valueForKey:@"teamName"];
      BOOL isChecked = [[item valueForKey:@"teamChecked"] boolValue];
  
      if (isChecked != 0)
      {
  
          NSManagedObjectContext *context = [event managedObjectContext];
          Team *team = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:context];
  
         [event addTeamsObject:team];
  
          team.teamName = name;
  
          NSError *error = nil;
          if (![context save:&amp;error]) 
          {
  
              NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
              abort();
          }
  
      }
  
  } 
  
  [self dismissModalViewControllerAnimated:YES];
  }
  </code></pre>
  
  <p>The problem with the code is that the insertNewObjectForEntity takes the team that is selected and creates a new duplicate team that is associated to the event.  How can I just associate the selected team(s) to the event? Any help is greatly appreciated!</p>