Cloud sync between iPad/iPhone appurl Tuesday, February 22, 2011
Cloud sync between iPad/iPhone app
If you can get away with it, the easiest way to do synchronization (by far) is to have three copies of your data locally: the copy you last uploaded ("old"), the copy produced by local changes ("mine") and the copy now downloaded from the server ("theirs").
Then, sort all the records in all three files and walk through them one by one:
- if old == mine, use theirs
- else if old == theirs, use mine
- else you have a conflict; do something about it (eg. always use mine, aka "last writer wins")
Note that "mine" or "theirs" or "old" might not exist. The rules above still apply in that case; if the result you choose is "does not exist", then you'll want to delete the record in the output file.
Finally, upload the resulting file back to the server so that it will be the "theirs" database for the next guy. Then copy the new file to your local "old" and "mine" databases.
(There are more space-efficient algorithms than the above... but there aren't any easier ones :) And disk space is pretty cheap nowadays, particularly if you compress the files.)