kotlin-extensions / io.realm.kotlin / io.realm.Realm / executeTransactionAwait


suspend fun Realm.executeTransactionAwait(context: CoroutineContext = Realm.WRITE_EXECUTOR.asCoroutineDispatcher(), transaction: (realm: Realm) -> Unit): Unit

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) {


context - optional CoroutineContext in which this coroutine will run.

transaction - the Realm.Transaction to execute.


IllegalArgumentException - if the transaction is null.

RealmMigrationNeededException - if the latest version contains incompatible schema changes.