Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Wrong Core Data Detail Record in Master-Detail

I defined a schema in Core Data Xcode with one-to-one detail records. In the Master Table View it displays the master records as expected. When I select a master record, it then displays a detail record, but for the wrong master. I viewed the actual data in SQLite and it looks fine. In the Master's PrepareForSegue routine the index that comes in is correct. The object selected is not. Do I need to do an additional Detail fetch?

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"showDetail"]) {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSLog(@"Master: Prepare for segue indexPath: %@", indexPath);
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];    
    NSLog(@"Master: Object before segue: %@", object);
     [[segue destinationViewController] setDetailItem:object];
    

    } }

Thanks!

1 Answer

I think I solved it. This line: NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath]; should be this line: NSManagedObject *object = [self.sessions objectAtIndex:indexPath.row]; Basically it prepares to load the passed object with the very same session object that was clicked on.