I’m stuck at ordering Core Data relationships. I have a to-many relationship but the problem I can’t overcome is that re-ordering relational entities in one view re-orders them in every view they belong to.
Example: People are added to activity1 and are randomly ordered using the moveRowAtIndexPath method. Those same people get added to activity2 and are ordered identically to activity1. How can I stop this?
Relevant code:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSUInteger fromRow = [fromIndexPath row];
NSUInteger toRow = [toIndexPath row];
id object = [[eventAthletes objectAtIndex:fromRow] retain];
[eventAthletes removeObjectAtIndex:fromRow];
[eventAthletes insertObject:object atIndex:toRow];
int i = 0;
for (object in eventAthletes)
{
[object setValue:[NSNumber numberWithInt:i++] forKey:@"athleteDisplayOrder"];
}
[self saveContext];
[object release];
}
I updated the model and made the attribute athleteDisplayOrder transient thinking that by using the key and NOT saving it, I would be able to start fresh in a new event. No Dice.
The identical ordering is expected behavior. If you want to “randomize” the order, you have to explicitly do that by reassigning random
athleteDisplayOrderattributes.EDIT:
If you want to have a special user definable order of athletes for each event one way to solve this is to create a new entity
EventAthletesthat has to-one relationships both Event and Athlete and also contains a display order attribute.END EDIT
BTW, you are using an extra data array with core data for display in a table view. Did you know that you can use
NSFetchedResultsControllerto manage your table view datasource much more efficiently?