@Override public boolean onConflict(Dataset dataset, List<SyncConflict> conflicts) { boolean r = super.onConflict(dataset, conflicts); if (observer != null) { observer.onDatasetDidSync(dataset.getDatasetMetadata().getDatasetName()); } return r; }
@Override public boolean onDatasetsMerged(Dataset dataset, List<String> datasetNames) { if (observer != null) { observer.onDatasetDidSync(dataset.getDatasetMetadata().getDatasetName()); } return super.onDatasetsMerged(dataset, datasetNames); }
@ReactMethod public void syncData(String datasetName, String key, String value) { Dataset dataset = cognitoClient.openOrCreateDataset(datasetName); dataset.put(key, value); dataset.synchronize(new DefaultSyncCallback()); }
public void doSync(boolean showDialog) { // Log.i("doInBackground", "Starting CognitoSyncTask with id: " // + cognito.getIdentityId()); ProgressDialog dialog = new ProgressDialog(callingActivity); if(showDialog) dialog = ProgressDialog.show(callingActivity,"Syncing", "Please wait"); Dataset dataset = AWSClientManager.getDataset(); printDataset(); dataset.synchronize(new SimpleCallback(dialog)); }
@Override public void onSuccess(Dataset dataset, final List<Record> newRecords) { printDataset(); callingActivity.runOnUiThread(new Runnable() { @Override public void run() { dialog.dismiss(); Log.i("Sync", String.format("%d records synced", newRecords.size())); Toast.makeText(callingActivity, "Sync Successful!", Toast.LENGTH_LONG).show(); } }); }
public void doSync() { ProgressDialog dialog = ProgressDialog.show(callingActivity, "Syncing", "Please wait"); Dataset dataset = AWSClientManager.getDataset(); printDataset(); dataset.synchronize(new SimpleCallback(dialog)); }
@Override public boolean onConflict(final Dataset dataset, final List<SyncConflict> conflicts) { callingActivity.runOnUiThread(new Runnable() { @Override public void run() { dialog.dismiss(); Log.i("Sync onConflict", String.format("%s records in conflict", conflicts.size())); List<Record> resolvedRecords = new ArrayList<Record>(); for (SyncConflict conflict : conflicts) { Log.i("Sync onConflict", String.format("remote: %s; local: %s", conflict.getRemoteRecord(), conflict.getLocalRecord())); /* resolve by taking remote records */ resolvedRecords.add(conflict.resolveWithRemoteRecord()); } dataset.resolve(resolvedRecords); Toast.makeText( callingActivity, String.format( "%s records in conflict. Resolve by taking remote records", conflicts.size()), Toast.LENGTH_LONG).show(); } }); return true; }
@Override public void onSuccess(Dataset dataset, List<Record> updatedRecords) { if (observer != null) { observer.onDatasetDidSync(dataset.getDatasetMetadata().getDatasetName()); } }
@Override public boolean onDatasetDeleted(Dataset dataset, String datasetName) { return false; }
void printDataset() { Dataset dataset = AWSClientManager.getDataset(); for (Record record : dataset.getAllRecords()) { Log.i("sync", record.toString()); } }
@Override public boolean onConflict(final Dataset dataset, final List<SyncConflict> conflicts) { callingActivity.runOnUiThread(new Runnable() { @Override public void run() { dialog.dismiss(); Log.i("Sync onConflict", String.format("%s records in conflict", conflicts.size())); List<Record> resolvedRecords = new ArrayList<Record>(); for (SyncConflict conflict : conflicts) { Log.i("Sync onConflict", String.format("remote: %s; local: %s", conflict.getRemoteRecord(), conflict.getLocalRecord())); /* resolve by taking remote records */ resolvedRecords.add(conflict.resolveWithRemoteRecord()); /* resolve by taking local records */ // resolvedRecords.add(conflict.resolveWithLocalRecord()); /* * resolve with customized logic, e.g. * concatenate strings */ // String newValue = // conflict.getRemoteRecord().getValue() // + conflict.getLocalRecord().getValue(); // resolvedRecords.add(conflict.resolveWithValue(newValue)); } dataset.resolve(resolvedRecords); Toast.makeText( callingActivity, String.format( "%s records in conflict. Resolve by taking remote records", conflicts.size()), Toast.LENGTH_LONG).show(); } }); return true; }
@Override public boolean onDatasetDeleted(Dataset arg0, String arg1) { return false; }
@Override public boolean onDatasetsMerged(Dataset arg0, List<String> arg1) { dialog.dismiss(); return false; }
public static Dataset getDataset() { return cognitosync.openOrCreateDataset(COGNITO_SYNC_DATASET_NAME); }
@Override public boolean onDatasetsMerged(Dataset arg0, List<String> arg1) { return false; }
/** * Opens or creates a dataset. If the dataset doesn't exist, an empty one * with the given name will be created. Otherwise, dataset is loaded from * local storage. If a dataset is marked as deleted but hasn't been deleted * on remote via refreshDatasetMetadata(), it will throw * IllegalStateException. * * @param datasetName a name of your dataset * @return an instance of dataset loaded from local storage */ public static Dataset openOrCreateDataset(String datasetName) { checkSyncAvailability(); return client.openOrCreateDataset(datasetName); }