What better way to work offline and synchronize data with server?

Asked

Viewed 7,407 times

21

I am working on a project where it will be necessary for users to register offline and then submit their registration to the server as soon as the internet connection is available.

My problem is knowing:

  • What is the best way to work offline? Preferably native Android.
  • When 2 users on different devices perform a modification to a person’s information, as I will update on the server?
  • If two people change the same record offline, the first one that syncs must have the change applied while the second one must fail (a revision number associated with each record can be used to verify this).

8 answers

20


The best way to keep information offline, on Android, is really the Sqlite.

But, for the update on the server, I believe the best procedure is to mark the update with a version code and a timestamp.

So, before sending the information to the server, you can check the configuration version and which of the competing versions is the most current.

You can also, on the server, create a rule of "merge" data. Preferring to overwrite the latest data.

Another practice that can be very good is to show the user that there is a data conflict and ask him which version should be maintained. An app that uses this method is Gameloft’s "My Favorite Evil" (or "Minion Rush").

  • I was racking my brain if I should duplicate lines with different versions of the same item, or try to come up with an update, more like I want freedom, 1 user working on more than one device or being able to have multiple users editing the same item, your advice fell right, the way then is to see everything, and the last is the main one. a conflict will be very rare or almost null, as users hardly edited the same person.

2

To work with offline registrations on Android should use the Sqlite, a good tutorial can be found here.

In cases of changing the same record, the ideal is that every time a change is detected in an outdated record, block the change, update the record on the device with the new data and warn the user that he should redo the change in that record.

I’m assuming you already have an API responsible for managing data sent by devices.

  • I’ve even created an App test, for me to devise the updates, I was even successful, I was able to update and send new and modified items, but when I wanted 1 ACCOUNT on 2 devices editing the same item, or 3 ACCOUNT doing the same, then it became much more complicated. I will consider your answer, as the tutorial is very good, but there is such a Neodatis, which is an OO bank with LGPL License, very good!!!

1

A SIMPLE and VERY EFFICIENT way to work with offline data and synchronization is use a database API Realtime. Because the API itself will keep your data synchronized, even offline, leaving you free of all the complexity of managing this data in two different databases.

I suggest using the Firebase, that works with major platforms such as Android, iOS and Web, and has a free plan (maximum connections 50, 5 GB data transfer, 100 MB of records in the database).

0

For the first problem use Sqlite.

The second problem can be solved with a Change Log on the server. When updating server information to the device, you should get the id of the last log record together with the alterable information, and when synchronizing the changed information with the server, check if the log log you have is equal to the last server record, if yes, change and write a new record in the log, if not, that means someone else has already made a change and included a new record, making your last log on the device less than the last log on the server.

0

Like previous responses, use Sqlite to save to your device.

Services on Android (Documentation)

When the application opens, start a service that will synchronize with the server when it has internet connectivity, so it will automatically sync without the need for the user to interact to send/receive data.

0

Regarding the second question, you could have in the comic book a field that, for example, would start as 1.

And offline they would make the changes, and then when they would go online they would check which of the two would have the largest field. If it was offline, then I would change the fields, if it was online nothing would happen.

Think of it as a version of a video game using an Auncher. Each time it starts, it connects to the BD to check that the version it has is equal to the one in the BD.

And of course who says with integers, also says with dates (it may even be easier to review later the date of change of the data).

I hope I helped x)

  • 4

    Mrw0lfe welcome to Stackoverflowpt. Your response looks more like a comment and doesn’t fit the site template. How about taking a Tour to get started ? http://answall.com/tour

0

Regarding the first question, another database alternative is the db4o.

It is object-oriented, despite the somewhat weak documentation, it is much faster and more practical to use than relational database. Of course there are exceptions and limitations, only I believe it’s worth taking a look and testing.

The second question cannot escape much of what has already been said. It is necessary to keep a record of the date of change. So when you have connection, make a comparison of the change date, to check which is more current.

0

If it’s different data where only id can create conflict, the ideal solution for this is to use id the UUID model, which generates a code that will never repeat itself and you don’t have to worry if someone has already sent data with the same id as yours.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.