Master Data NSFetchRequest returns an unsorted array after deleting an object and reprocessing the data

I have a Core Data database built using a UIManagedDocument, which I load into a UITableView , and also plot some points of this data on a graph. I find that when I add an object to the database or delete the object from the database and then retrieve the data, the array returned by NSFetchRequest is not sorted, although I have a sort descriptor in the request. The interesting part is that if I wait a few seconds and then extract / confirm the data, it will now be returned sorted. I think I should be doing something wrong. Is there some kind of callback I should use from Core Data to find out that the database change has been completed? This is strange because all the data is always in the database, just not sorted. I am not very experienced with this, so I am not sure.

Here is the code from my select query:

 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]; 
+6
source share
1 answer

Do you use controllerDidChangeContent: callback?

Perhaps use this to refresh your view, rather than doing it procedurally after saving the context.

0
source

Source: https://habr.com/ru/post/908277/


All Articles