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:

1do {
2 try await Amplify.DataStore.clear()
3 print("DataStore cleared")
4} catch let error as DataStoreError {
5 print("Error clearing DataStore: \(error)")
6} catch {
7 print("Unexpected error \(error)")
8}

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().) You can also explicitly begin the process with DataStore.start():

1do {
2 try await Amplify.DataStore.start()
3 print("DataStore started")
4} catch let error as DataStoreError {
5 print("Error starting DataStore: \(error)")
6} catch {
7 print("Unexpected error \(error)")
8}

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

1do {
2 try await Amplify.DataStore.stop()
3 print("DataStore stopped")
4} catch let error as DataStoreError {
5 print("Error stopping DataStore: \(error)")
6} catch {
7 print("Unexpected error \(error)")
8}