Modify an Object
To modify an object stored within a realm:
Open a write transaction with
realm.write()orrealm.writeBlocking().Query the transaction's mutable realm with
realm.query(). Specify the object type as a type parameter passed toquery(). To ensure your query returns the correct object, filter with unique identifying information such as a primary key value.Change an object property within the write transaction. The SDK automatically persists changes to the realm.
realm.write {
// fetch a frog from the realm by primary key
val frog: Frog? =
this.query<Frog>("_id == $0", PRIMARY_KEY_VALUE).first().find()
// modify the frog's age in the write transaction to persist the new age to the realm
frog?.age = 42
}
note
You can only modify objects in a realm within a write transaction.