Page updated Jan 16, 2024

Additional DataStore methods

Clear

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

1Amplify.DataStore.clear(
2 () -> Log.i("MyAmplifyApp", "DataStore cleared"),
3 error -> Log.e("MyAmplifyApp", "Error clearing DataStore", error)
4);
1Amplify.DataStore.clear(
2 { Log.i("MyAmplifyApp", "DataStore cleared") },
3 { Log.e("MyAmplifyApp", "Error clearing DataStore", it) }
4)
1try {
2 Amplify.DataStore.clear()
3 Log.i("MyAmplifyApp", "DataStore cleared")
4} catch (error: DataStoreException) {
5 Log.e("MyAmplifyApp", "Error clearing DataStore", error)
6}
1RxAmplify.DataStore.clear()
2 .subscribe(
3 () -> Log.i("MyAmplifyApp", "DataStore cleared"),
4 error -> Log.e("MyAmplifyApp", "Error clearing DataStore", error)
5 );
6}

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():

1Amplify.DataStore.start(
2 () -> Log.i("MyAmplifyApp", "DataStore started"),
3 error -> Log.e("MyAmplifyApp", "Error starting DataStore", error)
4);
1Amplify.DataStore.start(
2 { Log.i("MyAmplifyApp", "DataStore started") },
3 { Log.e("MyAmplifyApp", "Error starting DataStore", it) }
4)
1try {
2 Amplify.DataStore.start()
3 Log.i("MyAmplifyApp", "DataStore started")
4} catch (error: DataStoreException) {
5 Log.e("MyAmplifyApp", "Error starting DataStore", error)
6}
1RxAmplify.DataStore.start()
2 .subscribe(
3 () -> Log.i("MyAmplifyApp", "DataStore started"),
4 error -> Log.e("MyAmplifyApp", "Error starting DataStore", error)
5 );
6}

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().

1Amplify.DataStore.stop(
2 () -> Log.i("MyAmplifyApp", "DataStore stopped"),
3 error -> Log.e("MyAmplifyApp", "Error stopping DataStore", error)
4);
1Amplify.DataStore.stop(
2 { Log.i("MyAmplifyApp", "DataStore stopped") },
3 { Log.e("MyAmplifyApp", "Error stopping DataStore", it) }
4)
1try {
2 Amplify.DataStore.stop()
3 Log.i("MyAmplifyApp", "DataStore stopped")
4} catch (error: DataStoreException) {
5 Log.e("MyAmplifyApp", "Error stopping DataStore", error)
6}
1RxAmplify.DataStore.stop()
2 .subscribe(
3 () -> Log.i("MyAmplifyApp", "DataStore stopped"),
4 error -> Log.e("MyAmplifyApp", "Error stopping DataStore", error)
5 );
6}