Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for getting objects into a write transaction #298

Open
cmelchior opened this issue Jun 21, 2021 · 1 comment
Open

API for getting objects into a write transaction #298

cmelchior opened this issue Jun 21, 2021 · 1 comment

Comments

@cmelchior
Copy link
Contributor

cmelchior commented Jun 21, 2021

When moving to Frozen Architecture it was clear that we needed a way to convert objects from frozen to live. This issue tracks the various proposals we had so far and any input towards it. If you have any input please add a comment or modify the top-level post.

val obj = realm.write { Person() }

// This should throw: https://github.com/realm/realm-kotlin/issues/297
realm.write { 
  obj.name = "Jane" 
}

Proposals (they are not exclusive):

1. Argument to write block

realm.write(obj) { liveObj -> 
  liveObj.name = "Jane"
}
  • [CM] This was part of the original proposal, but after trying it out, I have become less of a fan. Mostly due to autocomplete in the IDEA not being great, i.e. the closure doesn't automatically fill in the live Object, so all you have is the "this" for the MutableRealm, this makes it very easy to accidentally keep referencing the obj inside the closure, which creates a runtime exception.

2. findLatest on MutableRealm

realm.write {
  findLatest(obj)?.apply {
    name = "Jane"
  }
}

3. write extension on the object itself

val wroteData: Boolean = obj.write {
  name = "Jane"
} 
  • [CM] While the syntax is very nice, it is possible to call this outside a write transaction, which would be wrong
  • [CM] It is also easy to forget to check if the write actually ran, which it might not, if the object was deleted by another device (which might, or might not, be a problem).

4. write extension on the object itself, but write control is outside

realm.write {
  val wroteData: Boolean = obj.write {
    name = "Jane"
  } 
}
@sync-by-unito
Copy link

sync-by-unito bot commented Dec 13, 2021

➤ Christan Melchior commented:

Moving to GA. Awaiting user feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant