```java public class ObserveExample { String tag = "ObserveQuery"; private Post post; public void observeExample() { Consumer> onQuerySnapshot = value ->{ System.out.println("value: " + value.getItems()); if (value.getItems().size() > 0) { post = value.getItems().get(0); } }; Consumer observationStarted = value ->{ Log.d(tag, "success on cancelable"); }; Consumer onObservationError = value ->{ Log.d(tag, "error on snapshot$value"); }; Action onObservationComplete = () ->{ Log.d(tag, "complete"); }; ObserveQueryOptions options = new ObserveQueryOptions(); Amplify.DataStore.observeQuery( Post.class, options, observationStarted, onQuerySnapshot, onObservationError, onObservationComplete ); } public void save() { // called from UI event, for example: Amplify.DataStore.save( post.copyOfBuilder() .title("new post") .build(), updated -> Log.i(tag, "updated post"), failure -> Log.e(tag, "update failed", failure) ); } } ``` ```kotlin class ObserveExampleKt { val tag = "ObserveQuery" var post: Post? = null fun observeExample() { val onQuerySnapshot: Consumer> = Consumer> { value: DataStoreQuerySnapshot -> Log.d(tag, "Post updated") post = value.items[0] } val observationStarted = Consumer { _: Cancelable -> Log.d(tag, "success on cancelable") } val onObservationError = Consumer { value: DataStoreException -> Log.d(tag, "error on snapshot $value") } val onObservationComplete = Action { Log.d(tag, "complete") } val options = ObserveQueryOptions() Amplify.DataStore.observeQuery( Post::class.java, options, observationStarted, onQuerySnapshot, onObservationError, onObservationComplete ) } fun save() { // called from a UI event, for example: post?.let { Amplify.DataStore.save(it.copyOfBuilder() .title("new title") .build(), { Log.i(tag, "Post saved") }, { Log.i(tag, "Error saving post") } ) } } } ``` ```kotlin // For coroutines, remember to `import com.amplifyframework.kotlin.core.Amplify` // instead of `import com.amplifyframework.core.Amplify` // and add "implementation 'com.amplifyframework:core-kotlin:0.20.0'" to gradle dependencies class ObserveExampleCoroutines { var post: Post? = null fun observeExample(lifecycleScope: LifecycleCoroutineScope) { lifecycleScope.async { Amplify.DataStore.observe( Post::class ).onStart { Log.i("MyAmplifyApp", "Observation began") } .catch { Log.e("MyAmplifyApp", "Observation failed", it) } .onCompletion { Log.i("MyAmplifyApp", "Observation complete") } .collect { post = it.item() Log.i("MyAmplifyApp", "Received post: $it") } } } fun save(lifecycleScope: LifecycleCoroutineScope) { lifecycleScope.async { post?.let { Amplify.DataStore.save(it) Log.i("MyAmplifyApp", "Post saved: $it") } } } } ``` ```java // Remember to import import com.amplifyframework.rx.RxAmplify; // and add "implementation 'com.amplifyframework:rxbindings:1.36.1'" to gradle dependencies public class ObserveExampleRx { private Post post; public void observeExample() { RxAmplify.DataStore.observe(Post.class) .subscribe( data -> { post = data.item(); Log.d("MyAmplifyApp", "Received post: " + post); }, failure -> Log.d("MyAmplifyApp", "Observe failed"), () -> Log.d("", "Observe complete") ); } public void save() { RxAmplify.DataStore.save(post.copyOfBuilder().title("New title").build()) .subscribe( () -> Log.d("MyAmplifyApp", "Save succeeded"), failure -> Log.d("MyAmplifyApp", "Save failed: " + failure) ); } } ```