Version: Smart Feature Phone 2.5

DataStore.sync()

The sync() method of the DataStore interface opens a cursor that allows you to step through any changes that have taken place in the data store going back to a particular revision ID, and run code in response to different types of change.

Syntax#

var cursor = stores[0].sync(3);

Returns#

A DataStoreCursor object.

Parameters#

  • revisionId Optional
    You can optionally pass sync() a revision ID that specifies the place in the revision history to go back to when running through the tasks. If you don't specify this, it runs through all tasks going back to the beginning of the history.

Example#

In the following example, we create a cursor using sync(), then invoke the runNextTask() function to run the next task in the cursor. In this function, the DataStoreCursor.next method is used to retrieve the next task (a DataStoreTask object), then the manageTask() function checks what type of task it is, and takes action accordingly. In this case, we are either done running through the tasks, in which case we return, or the task is an addition, in which case we run a function to display that addition in the app UI then move on to the next task by running runNextTask() again.

var cursor = stores[0].sync();
runNextTask(cursor);
function runNextTask(cursor) {
cursor.next().then(function(task) {
manageTask(cursor, task);
});
}
function manageTask(cursor, task) {
if (task.operation == 'done') {
// Finished adding contacts!
return;
}
if (task.operation == 'add') {
// Add the contacts that are different to how it was before
displayExisting(task.id, task.data);
}
runNextTask(cursor);
}