Reputation: 1755
Have a UITableViewController
to display scores from a game.
The scores will display 1 ,5, 1, 13
The order they were saved in, but after looking at the scores two or three times the scores they just start changing order. The scores will display
5, 1, 1, 13 or 1, 13, 1, 5
I save the scores using this code
-(void)saveDate
{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newScore = [NSEntityDescription insertNewObjectForEntityForName:@"Scores" inManagedObjectContext:context];
NSNumber *theScore = [[NSNumber alloc]initWithInt:self.score];
[newScore setValue:theScore forKeyPath:@"score"];
}
My entity name is "Scores" and I have have one attribute in it called "scores".
I load the Core Data
in the TableView
inside ViewDidLoad
:
- (void)viewDidLoad
{
[super viewDidLoad];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Scores"];
self.scores = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
self.scores is a @property
NSMutableArray
that I copy the data into so I use in the NumberOfRows
I use [self.scores count]
;
This is the code for my managed object context.
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
To display the cells I use this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *number = [self.scores objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"Score: %@", [number valueForKey:@"score"]]];
}
I use this exact code for another app and the data in the table always displays in the exact order it was saved. I have read a lot of the documentation and searched other places before using the method for an answer. I do understand why the information is changing order. Thanks for your help!
Upvotes: 1
Views: 312
Reputation: 33428
Few notes about your code.
First to retrieve an ordered result you should use a sort descriptor against your score attribute.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"score"
ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
Second, do NOT run a request without passing nil for the error and checking the returned value.
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
// Handle the error.
}
Finally, take advantage of NSFetchedResultsController
when dealing with UITableView
s.
Upvotes: 0
Reputation: 2427
You need to set the sortDescriptors of your NSFetchRequest. I can't answer why the same code in another app is returning the data in the same order every time. Nothing I've seen in the docs says anything about the order of returned results during a fetch request that doesn't have a sort descriptor attached. For example if you had an entity with a property called 'name' you would want something like
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
Upvotes: 3