Update Realm Objects - Kotlin SDK
Note
You can only modify objects in a realm within a write transaction.
Update Object Properties
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 }
Update a Dictionary Property
You can update a RealmDictionary as you would a Kotlin Map.
// Find frogs who have forests with favorite ponds val frogs = realm.query<Frog>().find() val frogsWithFavoritePonds = frogs.query("favoritePondsByForest.@count > 1").find() val thisFrog = frogsWithFavoritePonds.first() // Update the value for a key if it exists if (thisFrog.favoritePondsByForest.containsKey("Hundred Acre Wood")) { realm.write { findLatest(thisFrog)?.favoritePondsByForest?.set( "Hundred Acre Wood", "Lily Pad Pond" ) } } // Add a new key-value pair realm.write { findLatest(thisFrog)?.favoritePondsByForest?.put("Sherwood Forest", "Miller Pond") }
Upsert Objects
The upsert operation either inserts a new instance of an object or updates an existing object that meets certain criteria.
To upsert into a realm:
Open a write transaction with realm.write() or realm.writeBlocking().
Insert an object with a primary key using copyToRealm(). If an object matching the primary key already exists, the Kotlin SDK updates the existing object. If no object exists that matches the primary key, the Kotlin SDK inserts a new object.
You can specify the UpdatePolicy to use when you upsert an object with an existing primary key:
UpdatePolicy.ALL
: Update all properties on any existing objects identified with the same primary key.UpdatePolicy.ERROR
: Disallow updating existing objects and instead throw an exception if an object already exists with the same primary key.
realm.write { // The ID of a particular frog can either already exist or be a new ObjectId val frogId = ObjectId() // If a frog matching the ID exists, update its properties, otherwise create it this.copyToRealm(Frog().apply { _id = frogId name = "Wirt" age = 4 species = "Greyfrog" owner = "L'oric" }, updatePolicy = UpdatePolicy.ALL) }
Update a Collection
To update a collection of objects in a realm:
Query a realm for a collection of objects with realm.query().
Open a write transaction with realm.write() or realm.writeBlocking().
Update elements of the set of RealmResults returned by the query.
val tadpoles: RealmQuery<Frog> = realm.query<Frog>("age > $0", 2) for (tadpole in tadpoles.find()) { realm.write { findLatest(tadpole)?.name = tadpole.name + " Jr." } }