简体   繁体   中英

Core Data NSFetchRequest returns unsorted array after deleting object and refetching data

I have a Core Data database built using a UIManagedDocument that I load into a UITableView and also plot certain points of that data on a graph. I find that when I add an object to the database or delete an object from the database and then fetch the data, the array that NSFetchRequest returns is not sorted, even though I have a sort descriptor in the request. The interesting part is that if I wait a few seconds and then fetch/refetch the data, it now comes back sorted. I think I must be doing something wrong. Is there some kind of callback I should be using from Core Data to know that the database change was completed? It's strange because all the data is always in the database, just not sorted. I'm not super experienced with it, so I'm not sure.

Here's the code from my fetch request:

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"FuelPurchase"];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateTimeStamp" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

NSError *error = nil;
[self.fuelPurchaseDatabase.managedObjectContext executeFetchRequest:request error:&error];

Are you using controllerDidChangeContent: callback?

Maybe use this to update your view instead of doing it procedurally after you save your context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM