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 :) 
How to sync the data from the same user across multiple devices
java;php;mysql;android development
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:Devices A and B both sync up to the cloud and download version 1 of an object.User edits the object on device A, then gets frustrated because WiFi is broken.User goes to device B and edits the data.Device B syncs with the cloud: both records are on version 1, so it uploads the data and increments the version to 2.User goes back to device A, which is now connected.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:The same user is editing on both devices, so deciding what to do should be fairly simple for the user.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:Force the local edits to the cloud, overriding the more recent version (this would now be version 3 in the example above).Discard local changes and accept the cloud version (2).Edit the local record then save and sync as version 3.