title: NSFetchRequest for 2-way to-many Relationship
body: <p>I recently started using CoreData and ran into a complicated (at least to me) fetch that I could not figure out. I searched around but had a hard time coming up with good terminology to search with, and thus had no results that sufficed. Here is a basic example of my coredata model:</p>
  
  <p><img src="https://i.stack.imgur.com/imURD.png" alt="enter image description here"></p>
  
  <p>What I am trying to do is execute a fetch to get all of the <strong>Persons that are a member of a particular Club</strong>. Example: <em>get all of the members from the Chess Club</em>. I could do this easily with SQL (have a third table that represents PersonInClub, do two joins to combine the two tables, and Select all the Persons Where Club.name = 'Chess Club') but I am struggling to implement this with a fetch request.</p>
  
  <p>My ultimate goal is to write an <code>NSFetchRequst</code> for an <code>NSFetchedRequestController</code>. This is why I cannot just do a fetch for the Club with the name <code>Chess Club</code> and then grab all of the members (unless I am misunderstanding something, can you do this and put the results in a controller?).</p>
  
  <p>Any insights are much appreciated!</p>
  
  <p>EDIT: Here is the current code for my NSFetchedResultController. Not sure how this will help though:</p>
  
  <pre><code>- (NSFetchedResultsController *)fetchedResultsController
  {
      if (_fetchedResultsController != nil) {
          return _fetchedResultsController;
      }
  
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  
      [fetchRequest setEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext]];
  
  
      NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];    
      [fetchRequest setSortDescriptors:@[sortDescriptor]];
  
      // This will give me the chess club but I want it's members!
      // I tried things like @"[self.name = %@ members]" and stuff to try and retrieve the members as the results
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.name = %@", @"Chess Club"];
      [fetchRequest setPredicate:predicate];
  
      // an array with just the Chess Club in it
      NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
  
      // Printing the members, The members are correct but again, they are not attached to the Controller, the actual club is
      NSSet *members = [results.lastObject members];
      NSMutableArray *memberNames = [NSMutableArray arrayWithCapacity:members.count];
      for (Person *p in [members allObjects])
          [memberNames addObject:p.firstName];
      NSLog(@"All Members: %@", memberNames); // prints all of the members accurantely
  
      NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
      aFetchedResultsController.delegate = self;
      self.fetchedResultsController = aFetchedResultsController;
  
      NSError *error = nil;
      if (![self.fetchedResultsController performFetch:&amp;error]) {
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          abort();
      }
  
      return _fetchedResultsController;
  }  
  </code></pre>