I am working on a regular iPhone application that retrieves data from a server (XML, JSON, etc.), and I am wondering what is the best way to implement data synchronization. The criteria are speed (less data exchange over the network), reliability (data recovery in the event of an update failure), offline access and flexibility (adapted when the database structure changes a bit like a new column). I know that it depends on the application and the application, but can you share your strategy / experience?
For me, I am thinking of something like this:
1) Save last modified date to iPhone
2) After starting, send a message, for example getNewData.php? lastModifiedDate = ...
3) The server will process and send back only the changed data from the last time.
4) This data is formatted as follows:
<+><data id="..."></data></+> // add this to SQLite/CoreData
<-><data id="..."></data></-> // remove this
<%><data id="..."><attribute>newValue</attribute></data></%> // new modified value
I donβt want to do <+>, β, <%> ... for each attribute, since that would be too complicated, so probably when you get the <%> field, I will just delete the data with the given identifier, and then add it again (it is assumed that here is not some kind of automatically increasing field).
5) After everything is downloaded and updated, I will update the Last Modified Date field.
The main problem with this strategy is: if the network drops, when I update something => The last changed date is not updated yet => the next time I restart the application, I will have to go through the same thing again, not to mention potential inconsistent data. If I use a temporary table to update and make everything atomic, it will work, but then again, if the update is too large (many data changes), the user must wait a long time until new data appears. Should I use Last-Modified-Date for each data field and update the data gradually?