Question

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 :)

Was it helpful?

Solution

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.

OTHER TIPS

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...

Licensed under: CC-BY-SA with attribution
scroll top