Sync User Data with Cognito Sync

Amazon Cognito Sync makes it easy to save mobile user data, such as app preferences or game state in the AWS Cloud without writing any backend code or managing any infrastructure. You can save data locally on users’ devices allowing your applications to work even when the devices are offline. You can also synchronize data across a user’s devices so that their app experience will be consistent regardless of the device they use.

The tutorial below explains how to integrate Sync with your app.

Project Setup#


You must complete all of the instructions on the Set Up the SDK for Android page before beginning this tutorial.

Initialize the CognitoSyncManager#

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

CognitoSyncManager client = new CognitoSyncManager(

For more information about Cognito Identity, see Authenticate Users with Amazon Cognito Identity.

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:

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.