Become a MacRumors Supporter for $25/year with no ads, private forums, and more!
  • Did you order new AirTags? We've opened a dedicated AirTags forum.

luke3

macrumors newbie
Original poster
Jun 6, 2012
23
0
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...
 
Last edited by a moderator:

luke3

macrumors newbie
Original poster
Jun 6, 2012
23
0
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.
 
Comment

MattInOz

macrumors 68030
Jan 19, 2006
2,760
0
Sydney
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.
 
Comment

Scott90

macrumors 6502
Jul 14, 2008
273
0
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.
 
Comment

luke3

macrumors newbie
Original poster
Jun 6, 2012
23
0
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.


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?
 
Comment

luke3

macrumors newbie
Original poster
Jun 6, 2012
23
0
oops hit reply on wrong thread

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.
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.