Why `RoomDatabase.withTransaction` is defined in `...
# room
o
Why
RoomDatabase.withTransaction
is defined in
RoomDatabase.android.kt
and not available commonly? Is the
db.useWriterConnection { it.immediateTransaction }
the way to go in shared code? It feels like a workaround, right?
@Transaction
in Dao isn't suitable in my case
p
Late but I think it is useful. Room Multiplatform has function
Transactor.withTransaction
(https://android.googlesource.com/platform//frameworks/support/+/refs/heads/androidx-main/[…]roidx/room/Transactor.kt?autodive=0%2F%2F%2F%2F%2F%2F%2F), you can get it as a receive parameter in
useWriterConnection
block.
Copy code
suspend fun <R> RoomDatabase.useWriterConnection(block: suspend (Transactor) -> R): R =
    withContext(getCoroutineScope().coroutineContext) { useConnection(isReadOnly = false, block) }
        .also { invalidationTracker.refreshAsync() }
For example:
Copy code
database.useWriterConnection {
    it.withTransaction(Transactor.SQLiteTransactionType.IMMEDIATE) {
         userDao.insertAll(entities)
    }
}
o
Thanks, I'll try to take a look at it when I'm working again on this topic