Two-way sync between iPhone application and web application

流过昼夜 提交于 2019-12-21 05:22:20

问题


I have a web application that relies on a MySQL database, for which I am thinking of building an iPhone app. This app would allow users to browse/insert/update/delete data on their account on the web application.

The easiest way would be to build the iPhone app simply as an interface for the web app, ie each operation requires to be connected to the web. However, I would like the iPhone app to have its own "offline" version of the database. This would not only allow the user to work offline, but would also provide a better experience, as having everything locally means a faster and more responsive app.

I would handle conflicts by using a timestamp and keeping the latest version (I already use soft-deletion, ie when the user deletes a record I simply set a flag), but I have not quite figured out how to handle insertions. Indeed, if the user inserts new items on both offline (iPhone app) and online (web app) databases, there will be a primary key conflict (all my tables have a INTEGER field with auto-increment as primary key). How can I handle this problem ?

I was thinking of having a two-colums primary key, one being the auto-incremented integer, and the second being specific to the "device". This way, new records added via the web app would have ID 1-web, 2-web, etc. and the ones created via the iPhone app 1-iphone, 2-iphone, etc... This would allow to merge the two databases without having conflicts. What do you think of this idea ?


回答1:


The simplest solution by far is to use a UUID.

A less elegant solution is to have iPhone ids occupy a defined subset of the key space. For instance, with a 64-bit key, each iPhone could be assigned a specific pattern of the highest 20 bits.



来源:https://stackoverflow.com/questions/3106191/two-way-sync-between-iphone-application-and-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!