I am querying for a Client NSManagedObject via Core Data in my app and displaying the result in a simple details UIView pushed onto my UINavigationsController stack - nothing complex or out of the ordinary.
When I navigate back out of this details page for this Client object I am also checking for updates to my Clients in the background and if there are some I update my data store quietly in a background thread on a different managed object context. This is working and I see the changes to the data appear in my Core Data store via my SQLite Database Browser so I know the data has been updated.
The problem is when I navigate back to that same Client I will not see t开发者_StackOverflow中文版he changes unless I completely close my app or select a different Client and then go back to the one I know has changes. It seems the last Client object fetch request is being cached by my managed object context.
How do I prevent this from happening?
You need to register for NSManagedObjectContextDidSaveNotification
passing the background NSManagedObjectContext
as the object and then call mergeChangesFromContextDidSaveNotification:
when the notification fires on your NSManagedObjectContext
on the main thread.
This is specified in the Core Data Programming Guide: Concurrency with Core Data. And dont forget to refresh the data in your view (if it is a table view call reloadData
)
This may be an overly simple solution, but are you reloading your data in viewWillAppear: ?
- (void)viewWillAppear
{
[self.tableview reloadData];
}
精彩评论