Multiple updates of same object

I am just starting with Realm. I use the Realm Kotlin SDK. Expanding on this information.

If I want to update different properties of the same frog at different times. E.g.

fun updateAge(age: Int)
{
  realm.write {
      val frog: Frog? =
          this.query<Frog>("_id == $0", currentFrogId).first().find()
      frog?.age = age
  }
}
fun updateName(name: String)
{
  realm.write {
      val frog: Frog? =
          this.query<Frog>("_id == $0", currentFrogId).first().find()
      frog?.name = name
  }
}

Do I have to do the query every time, or is it possible to perform it once and use the result for multiple updates?

It depends on how your code is crafted.

If for example, you were to query the frog at keep it alive at a high level, like a class var, then any functions could update that frog at any time, as long as the frog doesn’t go out of scope.

Likewise, if you were to query a bunch of frogs and keep the returned results as a class var then any of those frogs could be updated at any time.

Another example would be if you were to keep the _id of the frog as a class var, you can update an object via it’s unique _id.