Modify an Object - Kotlin SDK
To modify an object stored within a realm:
- Open a write transaction with realm.write() or realm.writeBlocking().
- Query the transaction's mutable realm with realm.query().
Specify the object type as a type parameter passed to
query()
. 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.