Trouble with Core Data

Discussion in 'iOS Programming' started by luke3, Jul 5, 2012.

  1. luke3, Jul 5, 2012
    Last edited: Jul 5, 2012

    luke3 macrumors newbie

    Joined:
    Jun 6, 2012
    #1
    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???
     
  2. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #2
    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.
     

Share This Page