execute Transaction Await
suspend fun Realm.executeTransactionAwait(context: CoroutineContext = Realm.WRITE_EXECUTOR.asCoroutineDispatcher(), transaction: (realm: Realm) -> Unit)
Content copied to clipboard
Suspend version of Realm.executeTransaction to use within coroutines.
Canceling the scope or job in which this function is executed does not cancel the transaction itself. If you want to ensure your transaction is cooperative, you have to check for the value of CoroutineScope.isActive while running the transaction:
coroutineScope.launch {
// insert 100 objects
realm.executeTransactionAwait { transactionRealm ->
for (i in 1..100) {
// all good if active, otherwise do nothing
if (isActive) {
transactionRealm.insert(MyObject(i))
}
}
}
}
Parameters
context
optional CoroutineContext in which this coroutine will run.
transaction
the Realm.Transaction to execute.
Throws
if the transaction
is null
.
RealmMigrationNeededException
if the latest version contains incompatible schema changes.