Skip to content

Instantly share code, notes, and snippets.

@AOrobator
Created April 7, 2018 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AOrobator/b571e6a967de5f9bb0d567623abfbf07 to your computer and use it in GitHub Desktop.
Save AOrobator/b571e6a967de5f9bb0d567623abfbf07 to your computer and use it in GitHub Desktop.
fun realmTransactionSync(transaction: (Realm) -> Unit) {
val realm = Realm.getDefaultInstance()
var canceled = false
realm.beginTransaction()
try {
transaction(realm)
} catch (e: Exception) {
e.printStackTrace()
realm.cancelTransaction()
canceled = true
}
if (!canceled) {
realm.commitTransaction()
}
realm.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment