Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

luke3

macrumors newbie
Original poster
Jun 6, 2012
23
0
Ok I am trying to grab a NSMutable Set. Yes I have a previous post on this but this is slightly different. I have A player entity and a team entity. It is set up as a one to many relationship... On a different view controller I added players to the team. Now I am trying to get that teams players to show up on a table view... I am fetching the information as follows.

Code:
    - (NSFetchedResultsController *)fetchedResultsController {
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }
    
    NSString *entityName = @"Team"; 
    NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);
    
    // 2 - Request that Entity
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
    
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    _managedObjectContext = delegate.managedObjectContext;
    
    // 4 - Sort it 
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"players"
                                                                                     ascending:NO
                                                                                      selector:@selector(localizedCaseInsensitiveCompare:)]];
    
    // 5 - Fetch it
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.managedObjectContext
                                                                          sectionNameKeyPath:nil 
                                                                                   cacheName:nil];
    _fetchedResultsController.delegate = self;
    
    return _fetchedResultsController;
}

Then on my cell for row at index path I am setting the Player object to the fetched results as follows
Code:
        Player *p = [_fetchedResultsController objectAtIndexPath:indexPath];
Then, I am setting the title of the cell like so.
Code:
    cell.textLabel = p.firstName;

I am getting the error
Code:
reason: 'to-many key not allowed her
I am wondering what am I doing wrong???
 
Last edited:
As you've got it shown your FetchRequest is returning Teams not players.

Generally with a FetchedResultsController you ask it for the entity you are most interested in. So if your TableView is showing a list of "Players" then set the FetchedResultsController entity to 'players' not for the 'team'.

You can then use the other settings of the fetch request to filter down to the just the results your interested in. I often find it's easier to build each view piece meal. Get one bit working then refine, refine, refine.

The refinement to look at is the Predicate. Have a look at NSPredicate. Fetched results will use the predicate to see if it should include the Entities in the results.So in your case does the Players team equal the team your focusing on something like this.

Code:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"team like '%@'", team];

With the Fetched Results the sorting is purely about the ordering of objects returned. So should be using something like the players name or number to sort the players.

If you had the fetched results set up like that then it will let you know if a new Player is added to the team, then the delegate methods will get called to prompt the Tableview update.

btw: you could have keep this all in the same thread. It helps people understand the what's already been discussed.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.