```java
QueryOptions queryOptions = null;
try {
queryOptions = Where.identifier(Post.class, "123");
} catch (AmplifyException e) {
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
}
if (queryOptions != null) {
Amplify.DataStore.query(Post.class, queryOptions,
matches -> {
if (matches.hasNext()) {
Post original = matches.next();
Post edited = original.copyOfBuilder()
.title("New Title")
.build();
Amplify.DataStore.save(edited,
updated -> Log.i("MyAmplifyApp", "Updated a post."),
failure -> Log.e("MyAmplifyApp", "Update failed.", failure)
);
}
},
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
);
}
```
```kotlin
Amplify.DataStore.query(Post::class.java, Where.identifier(Post::class.java, "123"),
{ matches ->
if (matches.hasNext()) {
val original = matches.next()
val edited = original.copyOfBuilder()
.title("New Title")
.build()
Amplify.DataStore.save(edited,
{ Log.i("MyAmplifyApp", "Updated a post") },
{ Log.e("MyAmplifyApp", "Update failed", it) }
)
}
},
{ Log.e("MyAmplifyApp", "Query failed", it) }
)
```
```kotlin
Amplify.DataStore.query(Post::class, Where.identifier(Post::class.java, "123"))
.catch { Log.e("MyAmplifyApp", "Query failed", it) }
.map { it.copyOfBuilder().title("New Title").build() }
.onEach { Amplify.DataStore.save(it) }
.catch { Log.e("MyAmplifyApp", "Update failed", it) }
.collect { Log.i("MyAmplifyApp", "Updated a post") }
```
```java
QueryOptions queryOptions = null;
try {
queryOptions = Where.identifier(Post.class, "123");
} catch (AmplifyException e) {
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
}
if (queryOptions != null) {
RxAmplify.DataStore.query(Post.class, queryOptions)
.map(matchingPost -> matchingPost.copyOfBuilder()
.title("New Title")
.build()
)
.flatMapCompletable(RxAmplify.DataStore::save)
.subscribe(
() -> Log.i("MyAmplifyApp", "Query and update succeeded."),
failure -> Log.e("MyAmplifyApp", "Update failed.", failure)
);
}
```
Models in DataStore are *immutable*. Instead of directly modifying the fields on a Model, you must use the `.copyOfBuilder()` method to create a new representation of the model.