Page updated Jan 16, 2024

Observe in real time

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.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

Observe model mutations in real-time

You can subscribe to changes on your Models. This reacts dynamically to updates of data to the underlying Storage Engine, which could be the result of GraphQL Subscriptions as well as Queries or Mutations that run against the backing AppSync API if you are synchronizing with the cloud.

1Amplify.DataStore.observe(Post.class,
2 cancelable -> Log.i("MyAmplifyApp", "Observation began."),
3 postChanged -> {
4 Post post = postChanged.item();
5 Log.i("MyAmplifyApp", "Post: " + post);
6 },
7 failure -> Log.e("MyAmplifyApp", "Observation failed.", failure),
8 () -> Log.i("MyAmplifyApp", "Observation complete.")
9);
1Amplify.DataStore.observe(Post::class.java,
2 { Log.i("MyAmplifyApp", "Observation began") },
3 {
4 val post = it.item()
5 Log.i("MyAmplifyApp", "Post: $post")
6 },
7 { Log.e("MyAmplifyApp", "Observation failed", it) },
8 { Log.i("MyAmplifyApp", "Observation complete") }
9)
1Amplify.DataStore.observe(Post::class)
2 .onStart { Log.i("MyAmplifyApp", "Observation began") }
3 .catch { Log.e("MyAmplifyApp", "Observation failed", it) }
4 .onCompletion { Log.i("MyAmplifyApp", "Observation complete") }
5 .collect { Log.i("MyAmplifyApp", "Post: ${it.item()}") }
1RxAmplify.DataStore.observe(Post.class)
2 .subscribe(
3 post -> Log.i("MyAmplifyApp", "Post: " + post),
4 failure -> Log.e("MyAmplifyApp", "Observation failed.", failure),
5 () -> Log.i("MyAmplifyApp", "Observation complete.")
6 );

Observe query results in real-time

observeQuery(...) returns an initial data set, similar to query(...), and also automatically subscribes to subsequent changes to the query.

The first snapshot returned from observeQuery will contain the same results as calling query directly on your Model - a collection of all the locally-available records. Subsequent snapshots will be emitted as updates to the dataset are received in the local store.

While data is syncing from the cloud, snapshots will contain all of the items synced so far and an isSynced status of false. When the sync process is complete, a snapshot will be emitted with all the records in the local store and an isSynced status of true.

In addition to typical real-time use cases, observeQuery can be used on app launch to show your customers an initial data set from the local store while new data is being synced from cloud.

observeQuery also accepts the same predicates and sorting options as query.

1String tag = "ObserveQuery";
2Consumer<DataStoreQuerySnapshot<Post>> onQuerySnapshot = value ->{
3 Log.d(tag, "success on snapshot");
4 Log.d(tag, "number of records: " + value.getItems().size());
5 Log.d(tag, "sync status: " + value.getIsSynced());
6};
7Consumer<Cancelable> observationStarted = value ->{
8 Log.d(tag, "success on cancelable");
9};
10Consumer<DataStoreException> onObservationError = value ->{
11 Log.d(tag, "error on snapshot$value");
12};
13Action onObservationComplete = () ->{
14 Log.d(tag, "complete");
15};
16QueryPredicate predicate =
17 Post.TITLE.beginsWith("post").and(Post.RATING.gt(10));
18QuerySortBy querySortBy = new QuerySortBy("post", "rating", QuerySortOrder.ASCENDING);
19List<QuerySortBy> sortByList = new ArrayList<QuerySortBy>();
20sortByList.add(querySortBy);
21ObserveQueryOptions options = new ObserveQueryOptions(predicate, sortByList);
22Amplify.DataStore.<Post>observeQuery(
23 Post.class,
24 options,
25 observationStarted,
26 onQuerySnapshot,
27 onObservationError,
28 onObservationComplete
29);
1val tag = "ObserveQuery"
2 val onQuerySnapshot: Consumer<DataStoreQuerySnapshot<Post>> =
3 Consumer<DataStoreQuerySnapshot<Post>> { value: DataStoreQuerySnapshot<Post> ->
4 Log.d(tag, "success on snapshot")
5 Log.d(tag, "number of records: " + value.items.size)
6 Log.d(tag, "sync status: " + value.isSynced)
7 }
8
9 val observationStarted =
10 Consumer { _: Cancelable ->
11 Log.d(tag, "success on cancelable")
12 }
13 val onObservationError =
14 Consumer { value: DataStoreException ->
15 Log.d(tag, "error on snapshot $value")
16 }
17 val onObservationComplete = Action {
18 Log.d(tag, "complete")
19 }
20 val predicate: QueryPredicate =
21 Post.TITLE.beginsWith("post").and(Post.RATING.gt(10))
22 val querySortBy = QuerySortBy("post", "rating", QuerySortOrder.ASCENDING)
23
24 val options = ObserveQueryOptions(predicate, listOf(querySortBy))
25 Amplify.DataStore.observeQuery(
26 Post::class.java,
27 options,
28 observationStarted,
29 onQuerySnapshot,
30 onObservationError,
31 onObservationComplete
32 )
1val querySortBy = QuerySortBy("post", "rating", QuerySortOrder.ASCENDING)
2 Amplify.DataStore.observeQuery(
3 Post::class,
4 ObserveQueryOptions(Post.TITLE.beginsWith("post").and(Post.RATING.gt(10)),
5 listOf(querySortBy))
6 ).onStart { Log.i("MyAmplifyApp", "Observation began") }
7 .catch { Log.e("MyAmplifyApp", "Observation failed", it) }
8 .onCompletion { Log.i("MyAmplifyApp", "Observation complete") }
9 .collect { value -> Log.i("MyAmplifyApp", "Blog: ${value.items.size}") }
1QueryPredicate predicate =
2 Post.TITLE.beginsWith("post")
3 .and(Post.RATING.gt(10))
4 ;
5QuerySortBy querySortBy = new QuerySortBy("post", "rating", QuerySortOrder.ASCENDING);
6List<QuerySortBy> sortByList = new ArrayList<QuerySortBy>();
7sortByList.add(querySortBy);
8ObserveQueryOptions options = new ObserveQueryOptions(predicate, sortByList);
9rxDataStore.observeQuery(Model.class, options).subscribe(
10 modelDataStoreQuerySnapshot -> {
11 Log.d("mySampleApp", "success on snapshot");
12 Log.d("mySampleApp", "number of records: " + modelDataStoreQuerySnapshot.getItems().size());
13 Log.d("mySampleApp", "sync status: " + modelDataStoreQuerySnapshot.getIsSynced());
14 });