7

I'm working on a small fitness app in which the user (whom is authenticated with the a server) could create their own workout plan which will be synced with a server.

The problem with this is that What if the user has installed the app on multiple devices then he edited a workout plan's title on device A when it was offline and then he edited the title of the same workout plan again on device B which was online so it synced with server. Then the user went online on device A which would then automatically sync with the server. This would mean the previous title will be synced with the server an user.

Details:

  • The user is authenticated with google sign in/fb sign in using their official sdks.

  • During Authenticating the following data is added to server: Id, name, gender, email, age, weight, height, followers, following, platform.

  • All the data including the user data is stored on a mysql database

  • All the data from the server is accessed using php

  • The user data on the android device is stored in Shared Preferences and the user Workout Plans data is stored in SQLite Database.

My question is, How do I normally sync the data of the same user Across multiple devices.. I already got an idea of how I might do this but I know that loots of apps before mine has come to the same problem and there might a normal/standard way of doing this? Thanks :)

2
  • what was you idea to handle this scenario??
    – A.s.ALI
    Commented Jun 27, 2019 at 14:10
  • this problem can get very tricky but in my case its very simple, you just add an updated_at field in each table and compare it with the tables on the server during syncing.
    – edwinj
    Commented Jul 8, 2019 at 8:07

2 Answers 2

8

There are really two concerns here, and they are mostly solved problems in the domain of distributed computing.

How do I detect data out of sync?

Your objects should have versioning built-in. Each time an object is saved to permanent storage (whether a relational DB, NoSQL, OODB, whatever it is) you increment the version.

If an app goes to synch its changes to the cloud and detects the version in the cloud does not match the version it is editing, it knows the data was edited in two locations without syncing up in between.

Example:

  1. Devices A and B both sync up to the cloud and download version 1 of an object.
  2. User edits the object on device A, then gets frustrated because WiFi is broken.
  3. User goes to device B and edits the data.
  4. Device B syncs with the cloud: both records are on version 1, so it uploads the data and increments the version to 2.
  5. User goes back to device A, which is now connected.
  6. App sees that it has version 1 of the record, but the cloud has version 2 - it would sync, except there are local changes. Oops! This is a problem and it was easily detected.

How do I resolve conflicts?

Depending on the nature of the data it may be easy or difficult to resolve this conflict.

Source control software works in a similar way, where multiple users may edit files independently. The server tracks revision IDs. When a user goes to sync up, it checks revisions to see if there is an update. Typically, if an update is not in conflict (i.e. the same lines were not changed by separate users) it can be resolved by the software automatically. Otherwise, it may require user intervention (different VCS strategies work slightly differently but the conceptual workflow is similar).

This is actually not too far different from the scenario you presented. There are two key factors here:

  1. The same user is editing on both devices, so deciding what to do should be fairly simple for the user.

  2. The data is discrete. Much like lines in a text file, these simple fields on a table are separate yet complete data entities.

One option would be to display a screen with the record, highlighting the conflicting fields that are different in the two versions. Give the following options:

  1. Force the local edits to the cloud, overriding the more recent version (this would now be version 3 in the example above).

  2. Discard local changes and accept the cloud version (2).

  3. Edit the local record then save and sync as version 3.

5
  • Thank you so much for the answer, thats actually a pretty good idea, my only other question is, would I have to create a "version" field for every data item I have? so in my example, would I have to create a version field for the user workout plan title, workout day, etc.
    – edwinj
    Commented Feb 24, 2016 at 19:36
  • @edwinj I'm use timestamps instead of versions - if the timestamp from the client is the same as server (like with version), the update is accepted, otherwise rejected. It's almost the same except I don't have to "increment" anything, simply update timestamp in the server each time the record is updated. I'm not sure yet if this has flaws, but so far it seems to work well.
    – User
    Commented Jun 5, 2016 at 13:12
  • 1
    @lxxzz yeh thats the final solution I used. I had a clone record for each record and it stored the current time in milliseconds. so for example, if there was a name field, I would also have a name_v field which will have the last edited time in milliseconds. when syncing, I just compare the local and server _v records.
    – edwinj
    Commented Jun 5, 2016 at 14:53
  • 1
    @Ixxzz conceptually, timestamps and revisions are basically the same: an increasing number used to determine relative ordering in a temporal context.
    – user22815
    Commented Jun 5, 2016 at 15:12
  • @lxxzz 4 years later, I’m thinking about implementing this. How do you handle if device A deletes something? Cloud gets message to delete, but device B has no way of knowing.
    – Coltuxumab
    Commented Apr 11, 2020 at 0:52
0

I can't help but to think that if you have a user that isn't connected on a device, then he updates his information on another device that is connected, then comes back to the first device, the first thing that's gonna happen is, that first device is gonna want to sync with the server. At that point, the disconnected app has to take what the server says or give the user the option to overwrite. My question would be, "how is the user coming to connect device B without connecting device A first?" Look at the way source control works... if you make changes at work, forget to sync, make changes at home and sync, when you come back to work, you're gonna have sync conflicts you have to resolve. Syncing multiple devices with a central database works the same way. I guess in order to get a more detailed answer you'd have to provide more specifics about the situation at hand. Depending on the circumstance, you could write in "merge logic," but that could get quite messy...

2
  • Last one in wins. Or, read from the server first, then apply your pending changes. Syncing devices doesn't require anywhere near the rigor that source code control does, or even OLTP for that matter. Commented Feb 24, 2016 at 0:51
  • @RobertHarvey - I wouldn't say "doesn't." It "shouldn't," but we don't know enough about his application to make that call.
    – Christine
    Commented Feb 24, 2016 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.