amazon-archives / amazon-cognito-js

Amazon Cognito Sync Manager for JavaScript
http://aws.amazon.com/cognito
Apache License 2.0
200 stars 83 forks source link

conflict on dataset synchronization #31

Closed Mweydert closed 8 years ago

Mweydert commented 8 years ago

Hi,

I need some help using Cognito Sync Manager synchronize feature. The context : I want to store 2 values (key and user) in a cognito dataset. The key value change very often (at each connection) when the user is initialized once and then remains the same.

As it is shown in the documentation, I have called my credentials, instantiated a new CognitoSyncManager, opened/created my dataset, put data in it and finaly synchronized my dataset.

//retrieve credentials
AWS.config.credentials.get(function(error){
  if(error) { console.log(error); }
  //instantiate CognitoSyncManager
  var syncManager = new AWS.CognitoSyncManager();
  syncManager.openOrCreateDataset('data', function(erro, dataset) {
    if(erro) { console.log(erro); }
    dataset.put('key', myKey, function(err) {
      if(err){ console.log(err); }
      dataset.put('user', myUser, function(er) {
        if(er) { console.log(er); }
        //synchronize all changes
        return synchronize(dataset, function(res){
          console.log(res);
        });
      });
    });
  });
});

At the first synchronization (when dataset is created) there is no problem, but at the second it begins to fail (409 Conflict error). Message returned by cognito : "{"message":"Current SyncCount for: user is: 1 not: 0"}".

It seems that synchronizing a dataset where some values change, but one or more kept the same values cause synchronization conflict that leads to failure after 4 attempts. My synchronization function :

function synchronize(dataset, callback) {
  dataset.synchronize({
    onSuccess: function(dataset, newRecords) {
      console.log("Dataset has been synchronized !");
      return;
    },
    onFailure: function(err) {
      console.error("Unable to synchronize dataset.")
      console.log(err);
      return;
    },
    onConflict: function(dataset, conflicts, callback) {
      var resolved = [];
      for(var i = 0; i < conflicts.length; i++) 
      {
        resolved.push(conflicts[i].resolveWithLocalRecord()); 
      }   
      dataset.resolve(resolved, function(err, data) {
        if(err) { console.log(err); return callback(false); }
        console.log("Conflict resolved with local record.");
        return callback(true);
      });
    },
    onDatasetDeleted: function(dataset, datasetName, callback) {
      console.log("Dataset deleted.");
      return callback(true);
    },
    onDatasetMerged: function(dataset, datasetNames) {
      console.log("Dataset merged.");
      return callback(false);
    }
  });
}

Is this an issue with the cognitoSyncManager or did I do something wrong ?

Thank you in advance for you help. With best regards, Michael

Mweydert commented 8 years ago

The issue was solved by doing a first synchronization before putting data in cognito. I close the case.