Synchronize data offline with Phonegap

0

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

Use as Storage of the LocalStorage App and remote MySQL database.

    
asked by anonymous 17.04.2017 / 17:02

1 answer

1

You can work with a conditional. The 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();

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

Then comes the sync. Do it in the following way (simpler way); Save a refresh time field, and compare your server's with the client's and always sync with the latest.

Another way to do: link

EDIT

I found this plugin: link

    
17.04.2017 / 17:09