Page updated Nov 9, 2023

Additional DataStore methods

Amplify Android v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Android to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Clear

To stop DataStore sync process and to clear local data from DataStore, use the clear method:

Amplify.DataStore.clear( () -> Log.i("MyAmplifyApp", "DataStore cleared"), error -> Log.e("MyAmplifyApp", "Error clearing DataStore", error) );
1Amplify.DataStore.clear(
2 () -> Log.i("MyAmplifyApp", "DataStore cleared"),
3 error -> Log.e("MyAmplifyApp", "Error clearing DataStore", error)
4);

If your app uses authentication, it is recommended to call DataStore.clear() on sign-in or sign-out to remove any user-specific data. In scenarios where a mobile device can be shared by several users, calling DataStore.clear() will ensure that data does not leak from one user to another.

Start

Synchronization starts automatically whenever you run any DataStore operation (query(), save(), delete(), observe().) You can also explicitly begin the process with DataStore.start():

Amplify.DataStore.start( () -> Log.i("MyAmplifyApp", "DataStore started"), error -> Log.e("MyAmplifyApp", "Error starting DataStore", error) );
1Amplify.DataStore.start(
2 () -> Log.i("MyAmplifyApp", "DataStore started"),
3 error -> Log.e("MyAmplifyApp", "Error starting DataStore", error)
4);

Stop

To stop the DataStore sync process, you can use DataStore.stop(). This will close the real time subscription connection when your app is no longer interested in updates. You will typically call DataStore.stop() just before your application is closed. You can also force your DataStore sync expressions to be re-evaluated at runtime by calling stop() followed by start().

Amplify.DataStore.stop( () -> Log.i("MyAmplifyApp", "DataStore stopped"), error -> Log.e("MyAmplifyApp", "Error stopping DataStore", error) );
1Amplify.DataStore.stop(
2 () -> Log.i("MyAmplifyApp", "DataStore stopped"),
3 error -> Log.e("MyAmplifyApp", "Error stopping DataStore", error)
4);