AWS Mobile
Developer Guide

Android: Sync Data with Amazon Cognito Sync


Amazon Cognito Sync is an AWS service and client library that enables cross-device syncing of application-related user data. You can use the Amazon Cognito Sync API to synchronize user profile data across devices and across login providers—Amazon, Facebook, Twitter/Digits, Google, and your own custom identity provider.

For instructions on how to integrate Amazon Cognito Sync in your application, see Amazon Cognito Sync Developer Guide.

Set Up the SDK

You must complete all of the instructions on the Android: Setup Options for the SDK page before beginning this tutorial.

Initialize the CognitoSyncManager

Pass your initialized Amazon Cognito credentials provider to the CognitoSyncManager constructor.

CognitoSyncManager client = new CognitoSyncManager( getApplicationContext(), Regions.YOUR_REGION, credentialsProvider);

For more information about Cognito Identity, see cognito-auth-legacy.

Syncing User Data

To sync unauthenticated user data:

  1. Create a dataset and add user data.

  2. Synchronize the dataset with the cloud.

Create a Dataset and Add User Data

Create an instance of Dataset. User data is added in the form of key/value pairs. Dataset objects are created with the CognitoSyncManager class which functions as a Cognito client object. Use the defaultCognito method to get a reference to the instance of CognitoSyncManager. The openOrCreateDataset method is used to create a new dataset or open an existing instance of a dataset stored locally on the device.

Dataset dataset = client.openOrCreateDataset("datasetname");

Cognito datasets function as dictionaries, with values accessible by key:

.. code-block:: java

String value = dataset.get("myKey"); dataset.put("myKey", "my value");

Synchronize Dataset with the Cloud

To synchronize a dataset, call its synchronize method.


All data written to datasets will be stored locally until the dataset is synced. The code in this section assumes you are using an unauthenticated Cognito identity, so when the user data is synced with the cloud it will be stored per device. The device has a device ID associated with it. When the user data is synced to the cloud, it will be associated with that device ID.

To sync user data across devices (using an authenticated identity), see Amazon Cognito Sync.