Syncing iOS Core Data with remote server which sends XML
My app parses XML from remote server and store the objects in Core Data(SQLite storage). So that user can browse the material when OFFLINE by reading from local storage.
User may make changes to objects when browsing offline which gets stored locally in Core Data SQLite store. Another User makes some changes to object on Remote server and it is stored there. Now when I detect internet connection, my app should sync my local storage with remote server. Which means remote server is updated with changes I made to my Core Data(SQLite storage) when I was offline and my local storage - Core Data(SQLite storage) needs to be updated with what ever changes other user made to remote server.
For example there is a forum and it is stored in my local storage so that I can read and reply when I am traveling. When later on internet is accessible. My app should automatically put all my replies stored in core data to remote server and also bring other posts on remote server into my local storage.
Remote server is sending XML which I'm parsing and storing in Coredata. My problem is how to sync it ? How both ways communication happens when there is a change? How to sync only data whi开发者_开发问答ch has changed and not to IMPORT whole remote server DB and vice-versa ?
I know one of the way to do it..
- add one more field to your local and server database. i.e. Timestamp.
- when user change data on the local database change the Timestamp to current time. do same on the server..i.e. When someone edit data on the server change Timestamp to current time.
- When user connects to internet... check local Timestamp to Server timestamp.. case 1 Both are same - Nothing to do case 2 local time > server time - use sql to get all the data having timestamp greater than server timestamp.. and upload it on the server... case 3 local < server .... get all the records greater than the local timestamp and add it to the local database..
I am not sure if there is any better way... but this surely works...
One solution could be
- iphone syncs the changes to the server
- server merges the new and old stuff
- iphone gets the new changes (from the merge) from the server
So let the server be the master which should know how to merge stuff and the clients should only download the data incrementally after some changes.
Transactions. You want to follow the ACID rules for transactions. Essentially you have to make sure that data has not be updated that you have not refreshed locally before altering your local write to update.
So the easiest way is have a user get the most recent update from the server, then overwrite that and make sure that with timestamps no othe update happens during that process. Even better yet, use blocking with threads to insure that nothing else happens.
If you Google transactions or ACID there will be a lot of info out there. It's a big area in RDBMS environments where many users can corrupt the data and locks must be held between writes and updates.
精彩评论