NSMutableSet Core Data (merged)

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

  1. luke3, Jul 4, 2012
    Last edited by a moderator: Jul 5, 2012

    luke3 macrumors newbie

    Joined:
    Jun 6, 2012
    #1
    I am using core data and set up a one to many relationship for one of my entities. I have two entities. "Team" and "Player" I am trying to add an NSMutableSet of players to the team.

    Below is how I am attempting to add a player to the team.
    Code:
        -(void)addPlayerButton {
        
        [_tempSet addObject:@""];
        
        NSLog(@"number of cells in _tempSet is:%i",[_tempSet count]);
        
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1]  withRowAnimation:UITableViewRowAnimationFade]; 
        
        }
    
    This is how I am saving
    Code:
        -(void)saveButtonWasPressed {
        
        self.team =[NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:self.managedObjectContext];
      
        self.player = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:self.managedObjectContext];
        [team addPlayersObject:player];
        
        team.schoolName = _schoolName.text;
        team.teamName = _teamName.text;
        team.teamID = _teamName.text;
        team.season =  _season.text;
        team.headCoach = _headCoach.text;
        team.astCoach = _assistantCoach.text;
        
        player.firstName = cell.playerFirstName.text;
        player.lastName = cell.playerLastName.text;
        player.number  = cell.playerNumber.text;
        
        [self.team addPlayers:_tempSet];
    
        
        [self.managedObjectContext save:nil];
        [self.navigationController popViewControllerAnimated:YES];    
    }
    
    There are two things going wrong, one, the _tempSet only adds one object and can not add anymore. and the second crashes when I click save right before the line [self.team addPlayers:_tempSet]; With the error [__NSCFConstantString _isKindOfEntity:]: unrecognized selector sent to instance 0xd7cd8'

    I am relatively new to Core Data so please feel free to correct me if I am doing something else wrong...
     
  2. luke3 thread starter macrumors newbie

    Joined:
    Jun 6, 2012
    #2
    Filling Tableview with NSMutableSet

    Hi I am using core data and trying to populate a tableview with an NSMutableSet. I have two entities, Teams and Players. On my addTeamsController I am saving a player to the team as follows
    Code:
        -(void)saveButtonWasPressed {
        
        self.team =[NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:self.managedObjectContext];
        
        Player *newPlayer = (Player *)[NSEntityDescription insertNewObjectForEntityForName:@"Player" 
                                                                    inManagedObjectContext:self.managedObjectContext];
        team.schoolName = _schoolName.text;
        team.teamName = _teamName.text;
        team.teamID = _teamName.text;
        team.season =  _season.text;
        team.headCoach = _headCoach.text;
        team.astCoach = _assistantCoach.text;
        
    
        **[self.team addPlayers:_tempSet];**
    
        [self.managedObjectContext save:nil];
        [self.navigationController popViewControllerAnimated:YES];    
    }
    
    On another viewController I am trying to populate a tableview with that teams players. To do that I am doing as follows
    Code:
            NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:NO];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
        _array = [[_team.players allObjects] sortedArrayUsingDescriptors:sortDescriptors];
    
    and then on my cell for row and index path I am doing the following
    Code:
            cell.textLabel.text = [_array objectAtIndex:indexPath.row];
    
    And I get the error
    Code:
        [Player  isEqualToString:]: unrecognized selector sent to instance
    
    I am wondering what the best approach to filling the tableview sorted by the players first names is.
     
  3. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #3
    First up is there are reason not to use NSFetchedResultsController to act as the controller between core data and tableview?

    NSFetchedResults will give you nice notifications of changes regardless of where you change the Core Data context.
     
  4. Scott90 macrumors 6502

    Joined:
    Jul 14, 2008
    #4
    The problem of it only adding one object is because you insert a constant value into the set (@""). NSSet's objects are unique, so the empty strings will not be added more than once.

    As for the error, I have the feeling it has to do with it being an empty string. Try giving it an actual value (even if it's constant), and see if you still have the same problem.
     
  5. luke3 thread starter macrumors newbie

    Joined:
    Jun 6, 2012
    #5

    When I set up a Fetched results controller it did not seem to work. I need to get the NSMutableSet of "Players" added to the Entity Team. So what would I fetch for?
     
  6. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
  7. luke3 thread starter macrumors newbie

    Joined:
    Jun 6, 2012
    #7
    Ok I am trying to sort a one to many relationship in core data. That is the basis of the problem.. I am wondering what the best approach to that is.
     

Share This Page