Can you please explain to me how should I implement the logic of syncing to my app? I mean, I have the sync code, I have the services, the provider, everything, but I do not understand it (I have the sync code from an Android Programming (OReilly) tutorial).
So, I imagine, the sync should happen like this:
- user starts the app, and is directed towards the login page, enters there a user and a password, then clicks LOGIN.
- Now, my login method sends a HTTP request to my server side (remote mySQL+PHP), and on the server side a PHP file returns a token in a JSON (if the user+pass exist).
- Now that I have the token in the app, I create an account (so I can use the sync feature of Android)
- Then if login successfull, I start the mainActivity and carry on with the regular job of the app.
- Now...
... so far, so good. My account appears in Accounts/Syncing
I Assume I should store the token on the server side (in the "users" remote table perhaps) Then I guess, I should implement my sync logic. Now here is my question: The sync logic, should it be something like this?
Remote table1 structure: id,name,surname,dateofbirth,deleted
Remote table2 structure: id, invoice_no,invoice_date, invoice_total,deleted
Local (SQLite) tablex1 structure: _id,name,surname,dateofbirth,deleted
Local (SQLite) tablex2 structure: _id, invoice_no,invoice_date, invoice_total,deleted
Should I add to local tables a new field like "remote_id" (integer)?
Then add to remote tables a new field called "synced" (boolean) or maybe a "synced-timestamp"?
And then have 2 methods: pull() (for getting data from remote) and push() (for sending data to remote)
onPerformSync should I make verifications like this:
(for push)
- send to remote a list with all records from tablex1 and tablex2 that have "remote_id" = null
- on the server side, the PHP (after checking the token to be valid) will insert the new rows, with "synced" set to true
(for pull)
- ask the remote to send back to me the list of all records that have the synced=false, then set them to true on remote.
- process the returned rows and insert them locally in SQLite (including "remote_id")
Is this a good approach? Is it prone to duplicates or other problems? What should I use as a logic?
What do you do for syncing?
What logic do you recomend?
Thank you