I have data that is persisted on the device that needs to sync with my my server, in terms of design am I on the right track?
I have a model with a method to return my local objects, let's call it [model objects]';
Within this method it returns the local objects immediately but I'd also like to check if the server needs to update any of these objects so in the background thread I run a method that uses an async NSUrlConnection
that will return the JSON of each object that needs to be updated in which I will save that to core data. In the Table View Controller I would set a KVO to observe when the values changed for the updatedObjects
then reload the objects displayed in the objectTableView
.
Question: Is this the correct way to handle this? Am I missing something? Can I improve this somehow?
I am also thinking of disabling the UITableViewCells of the objects that are being updated and showing a UIActivityIndicator
during the downloading and saving stages but I'm not sure if this will cause any race conditions in the UX.
Hopefully this was explained well enough for you to understand, if you have any questions I will try to reply immediately.
There is support for this sort of problem within CoreData. And there are a variety of approaches.
The basic gist is that you create a new context in your background thread for your async web call, then comparing your local data to the new received data you manage updates. Then you perform a merge from your thread context to your main context. At this point you can receive notification of the updated data in CoreData and update your views.
You can even use NSFetchedResultsController
to simplify this even more.