开发者

A way to differentiate datas when synchronize db?

I have a web app which I can create some notes, each time I create a new note, it will insert to a table with an auto_increment id. (quite obvious)

Now I want to develop an android app which I can create notes too (save them locally in sqlite), and then syncronize those notes with the server.

The problem is, when I create notes in my phone they will have their own auto_increment id which many times will be the same with those notes in server!

I don't care to have duplicated notes 开发者_JAVA技巧(actually I don't think there is a way to differentiate if the new note is duplicated or not, because they don't have some physical id), the problem is if they have same id (primary key), I won't be able to insert them to the server.

Any suggestion?


You could use an UUID as a key for your note.

That way, each entry should have an unique id, be it created on the server or on the client.

To create a UUID, you can use UUID.randomUUID().


The most obvious solution would be to give each note its own unique hash or GUID in addition to the database's auto_increment_id.

You'd then use these unique values as the basis for synchronisation in conjunction with a "last synced" timestamp in each of the tables so that you know what data needs to be synced and can easily determine if the data already exists in the destination (and should be updated) or whether it's a new note.


I'm sorry but i think that your DB structure is wrong. You cannot use autoincrement field in this way, different DBs with a disconnected architecture. Autoincrement values are created for a specific use, if you need to merge two tables like this, you have to implement a different logic. Use a note_id to identify a note in a unique way, using more data (i.e. the user id, the device id etc.) to make this id unique. Autoincrement will only give you a messy architecture at best in this scenario

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜