Hi guys, :wave: I was wondering if it is possible ...
# android-architecture
n
Hi guys, 👋 I was wondering if it is possible to execute a room db transaction in the a usecase. Is it a good idea, I was thinking of creating a dao that looks like this
Copy code
@Dao
interface EntityDao {
    @Transaction
    fun transaction(block: () -> Unit) {
        block()
    }
}
And expose this to the repository layer then the usecase.
For unit tests it would be easier to replace it
n
Thanks @rattleshirt, 👋 I ended up using the writeConnection block from RoomDatabase to execute transactions, and I realize it is a bad thing to make this accessible in the usecase since it is not flexible in changing datasource, we cant use transactions for remote datasource for example. I need to look into sqldelight and to the links you sent, I feel like everyone is using it sqldelight.
r
Oh you don't have to use sqldelight per se. It is more about the concept of having a transaction runner injectable
n
I use this one,
Copy code
suspend fun writeTransaction(block: suspend () -> Unit) {
        db.useWriterConnection { transactor ->
            transactor.immediateTransaction {
                block()
            }
        }
    }