What primary key to install on the local Android database before synchronizing with the server?

I am making an application with a database model:

Item (int id, String name, String description, Date updatedDate, boolean isSynced)

id Itemis the primary key (both on the local and on the server) and is idautomatically generated on the mysql db server. I use realm to store Itemson an Android device.

The user can create and edit Itemson the device using / without the Internet. DB synchronizes when an internet connection is available.

Therefore, when the user saves a new one Item, then you idshould save it on a local db so that synchronization with the server does not cause problems.

An example :

John has 3 Itemson his Moto X as well as the Samsung S6 , and both phones synchronize with the server.

It puts the Moto X in airplane mode and adds it Itemto the Samsung S6 , which syncs with the server.

Now S6 has elements with identifiers → 1,2,3,4

and Moto X has elements with identifiers → 1,2,3

When the Moto X connects to the Internet, it will boot Itemusing id= 4, but it is still in airplane mode.

Moto X , Item. Item db, Moto X? Item Moto X, .

+4
1

spitballing...

int local_id int device_id Item. , , device_id. , local_id, . (! IsSynced && id == null), , DB, local_id device_id, , Item id Item isSynced true.

ETA. db, , local_id .

ETA2: , local_id , id UNIQUE. , , id db.

+4

Source: https://habr.com/ru/post/1606048/


All Articles