title: Core Data: Fetch all entities in a to-many-relationship of a particular object?
body: <p>in my iPhone application I am using simple Core Data Model with two entities (<strong>Item</strong> and <strong>Property</strong>):</p>
  
  <p><strong>Item</strong><br>
  name<br>
  <em>properties</em>  </p>
  
  <p><strong>Property</strong><br>
  name<br>
  value<br>
  <em>item</em>  </p>
  
  <p><strong>Item</strong> has one attribute (name) and one one-to-many-relationship (<em>properties</em>). Its inverse relationship is <em>item</em>. <strong>Property</strong> has two attributes the according inverse relationship.</p>
  
  <p>Now I want to show my data in table views on two levels. The first one lists all items; when one row is selected, a new UITableViewController is pushed onto my UINavigationController's stack. The new UITableView is supposed to show all properties (i.e. their names) of the selected item. </p>
  
  <p>To achieve this, I use a <code>NSFetchedResultsController</code> stored in an instance variable. On the first level, everything works fine when setting up the NSFetchedResultsController like this: </p>
  
  <pre><code>-(NSFetchedResultsController *) fetchedResultsController {
      if (fetchedResultsController) return fetchedResultsController;
  
      // goal: tell the FRC to fetch all item objects.
      NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
  
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.moContext];
      [fetch setEntity:entity];
  
      NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
      [fetch setSortDescriptors:[NSArray arrayWithObject:sort]];
  
      [fetch setFetchBatchSize:10];
  
      NSFetchedResultsController *frController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetch managedObjectContext:self.moContext sectionNameKeyPath:nil cacheName:@"cache"];
      self.fetchedResultsController = frController;
      fetchedResultsController.delegate = self;
  
      [sort release];
      [frController release];
      [fetch release];
  
      return fetchedResultsController;
  }
  </code></pre>
  
  <p>However, on the second-level UITableView, I seem to do something wrong. I implemented the fetchedresultsController in a similar way:</p>
  
  <pre><code>-(NSFetchedResultsController *) fetchedResultsController {
      if (fetchedResultsController) return fetchedResultsController;
  
      // goal: tell the FRC to fetch all property objects that belong to the previously selected item
      NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
  
      // fetch all Property entities.
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"Property" inManagedObjectContext:self.moContext];
      [fetch setEntity:entity];
  
      // limit to those entities that belong to the particular item
      NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"item.name like '%@'",self.item.name]];
      [fetch setPredicate:predicate];
  
      // sort it. Boring.
      NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
      [fetch setSortDescriptors:[NSArray arrayWithObject:sort]];
  
      NSError *error = nil;
      NSLog(@"%d entities found.",[self.moContext countForFetchRequest:fetch error:&amp;error]);
      // logs "3 entities found."; I added those properties before. See below for my saving "problem".
      if (error) NSLog("%@",error);
      // no error, thus nothing logged.
  
      [fetch setFetchBatchSize:20];
  
      NSFetchedResultsController *frController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetch managedObjectContext:self.moContext sectionNameKeyPath:nil cacheName:@"cache"];
      self.fetchedResultsController = frController;
      fetchedResultsController.delegate = self;
  
      [sort release];
      [frController release];
      [fetch release];
  
      return fetchedResultsController;
  }
  </code></pre>
  
  <p>Now it's getting weird. The above <code>NSLog</code> statement returns me the correct number of properties for the selected item. However, the UITableViewDelegate method tells me that there are no properties:</p>
  
  <pre><code>-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
      id &lt;NSFetchedResultsSectionInfo&gt; sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
      NSLog(@"Found %d properties for item \"%@\". Should have found %d.",[sectionInfo numberOfObjects], self.item.name, [self.item.properties count]);
      // logs "Found 0 properties for item "item". Should have found 3."
      return [sectionInfo numberOfObjects];
  }
  </code></pre>
  
  <p>The same implementation works fine on the first level. </p>
  
  <p>It's getting even weirder. I implemented some kind of UI to add properties. I create a new <strong>Property</strong> instance via <code>Property *p = [NSEntityDescription insertNewObjectForEntityForName:@"Property" inManagedObjectContext:self.moContext];</code>, set up the relationships and call <code>[self.moContext save:&amp;error]</code>. This seems to work, as <code>error</code> is still <code>nil</code> and the object gets saved (I can see the number of properties when logging the <strong>Item</strong> instance, see above). However, the delegate methods are not fired. This seems to me due to the possibly messed up fetchRequest(Controller). </p>
  
  <p>Any ideas? Did I mess up the second fetch request? Is this the right way to fetch all entities in a to-many-relationship for a particular instance at all?</p>