Sync data offline with Phonegap

Asked

Viewed 196 times

0

A situation has arisen that I need to synchronize data from an App but the device does not always have connectivity because it is used by a seller on the street. When it has connectivity it saves right via AJAX but when it does not have it is the problem.

Use as App storage Localstorage and Mysql remote database.

1 answer

1

You can work with a conditional. Cordova has the connection plugin as below.

function checkConnection() {
    var networkState = navigator.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';

    alert('Connection type: ' + states[networkState]);
}

checkConnection();

Every time there is connectivity, send it to the server. If not, send it to locastorage.

Then comes the synchronization. Do it this way (simplest way); Save an update time field, and compare your server with the client and always check the latest.

Another way of doing: http://coenraets.org/blog/2012/05/simple-offline-data-synchronization-for-mobile-web-and-phonegap-applications/

EDIT

I found this plugin: https://github.com/phonegap/phonegap-plugin-contentsync

  • Hmmm... ta giving a light. In case this check should be from time to time, at the time you press the save button or before opening the registration?

  • It may be from time to time, but depending on the number of requests it would overload the server. So beware

  • this last plugin that you put me seemed to sync files, proceeds?

  • That, I never used it, but I suspect that if you pass a URL it will also work. If it doesn’t work, let me know that I edit the answer

Browser other questions tagged

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