Not clear how to use coroutines extensions (executeTransactionAwait)

Hi,

Could you please describe how to use (or ideas behind of) kotlin-coroutines extensions?

In particular, I open realm and run executeTransactionAwait on IO dispatcher, and it throws

java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created

The code is similar to following

withContext(Dispatchers.IO) {
	openRealm().use { realm ->
		realm.executeTransactionAwait {
			runTransaction()
		}
	}
	emitUpdate(value)
}

PS. We’re using MongoDB Realm 10.3.1 in the way that we don’t keep Realm opened, but instead for each transaction:
Open realm → execute transaction → close realm

2 Likes

Was you able to fix it? i have the same doubt

No, we have abandoned built-in extensions and use custom code

executeTransactionAwait will call Realm.refresh() on current CoroutineContext to ensure current realm get latest changed,
so use withContext(Dispatchers.Main) or withContext(SingleThreadExecutor().asCoroutineDispatcher())