com.amazonaws.mobileconnectors.cognito
Class DefaultSyncCallback

java.lang.Object
  extended by com.amazonaws.mobileconnectors.cognito.DefaultSyncCallback
All Implemented Interfaces:
Dataset.SyncCallback

public class DefaultSyncCallback
extends java.lang.Object
implements Dataset.SyncCallback

A default implementation of SyncCallback. All methods but onConflict are stubbed with logging. onConflict resolves conflicts using a 'last writer wins' strategy


Constructor Summary
DefaultSyncCallback()
           
 
Method Summary
 boolean onConflict(Dataset dataset, java.util.List<SyncConflict> conflicts)
          This can be triggered during two phases.
 boolean onDatasetDeleted(Dataset dataset, java.lang.String datasetName)
          This is triggered when the given dataset is deleted remotely.
 boolean onDatasetsMerged(Dataset dataset, java.util.List<java.lang.String> datasetNames)
          If two or more datasets are merged as a result of identity merge, this will be triggered.
 void onFailure(DataStorageException dse)
          This is called when an exception occurs during sync.
 void onSuccess(Dataset dataset, java.util.List<Record> updatedRecords)
          This is called after remote changes are downloaded to local storage and local changes are uploaded to remote storage.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultSyncCallback

public DefaultSyncCallback()
Method Detail

onSuccess

public void onSuccess(Dataset dataset,
                      java.util.List<Record> updatedRecords)
Description copied from interface: Dataset.SyncCallback
This is called after remote changes are downloaded to local storage and local changes are uploaded to remote storage. Updated records from remote storage are passed in the callback. If conflicts occur during synchronize and are resolved in Dataset.SyncCallback.onConflict(com.amazonaws.mobileconnectors.cognito.Dataset, java.util.List) after several retries, then updatedRecords will be what are pulled down from remote in the last retry.

Specified by:
onSuccess in interface Dataset.SyncCallback
Parameters:
dataset - the dataset that performed sync
updatedRecords - new records from remote storage that are downloaded

onConflict

public boolean onConflict(Dataset dataset,
                          java.util.List<SyncConflict> conflicts)
Description copied from interface: Dataset.SyncCallback
This can be triggered during two phases. One is when the remote changes are about to be written to local storage. The other is when local changes are uploaded to remote storage and got rejected. Here is an example:
 List<Record> resolved = new ArrayList<Record>();
 for (SyncConflict conflict : conflicts) {
     resolved.add(conflicts.resolveWithRemoteRecord());
 }
 dataset.save(resolved);
 return true; // so that synchronize() can retry
 
If you prefer to add additional logic when resolving conflict, you can use SyncConflict.resolveWithValue(String)
 int remoteMoney = Integer.valueOf(conflicts.getRemote().getValue());
 int localMoney = Integer.valueOf(conflicts.getLocal().getValue());
 int total = remoteMoney + localMoney;
 Record resolve = conflicts.resolveWithValue(String.valueOf(total));
 

Specified by:
onConflict in interface Dataset.SyncCallback
Parameters:
dataset - the dataset that performed sync
conflicts - conflicting records
Returns:
true if conflicts are resolved so that synchronize will retry, false otherwise.

onDatasetDeleted

public boolean onDatasetDeleted(Dataset dataset,
                                java.lang.String datasetName)
Description copied from interface: Dataset.SyncCallback
This is triggered when the given dataset is deleted remotely. Return true if you want to remove local dataset, or false if you want to keep it.

Specified by:
onDatasetDeleted in interface Dataset.SyncCallback
Parameters:
dataset - dataset handler
datasetName - the name of the dataset that is deleted remotely
Returns:
true to remove local dataset, or false to keep it

onDatasetsMerged

public boolean onDatasetsMerged(Dataset dataset,
                                java.util.List<java.lang.String> datasetNames)
Description copied from interface: Dataset.SyncCallback
If two or more datasets are merged as a result of identity merge, this will be triggered. A list of names of merged datasets' is passed in. The merged dataset name will be appended with its old identity id. One can open the merged dataset, synchronize the content, reconcile with the current dataset, and remove it. This callback will fire off until the merged dataset is removed.

Specified by:
onDatasetsMerged in interface Dataset.SyncCallback
Parameters:
dataset - dataset handler
datasetNames - a list of names of merged datasets'
Returns:

onFailure

public void onFailure(DataStorageException dse)
Description copied from interface: Dataset.SyncCallback
This is called when an exception occurs during sync.

Specified by:
onFailure in interface Dataset.SyncCallback
Parameters:
dse - exception


Copyright © 2010 Amazon Web Services, Inc. All Rights Reserved.