Hi, I'm wondering if there is a way to implement s...
# touchlab-tools
l
Hi, I'm wondering if there is a way to implement something like suspend fun Transacter.transactionWithContext, but that would return result. I tried something like this, but I feel stuck In Couroutine Extensions:
Copy code
suspend fun <T: Any> Transacter.transactionWithContextResult(
    coroutineContext: CoroutineContext,
    noEnclosing: Boolean = false,
    body: TransactionWithReturn<T>.() -> T
): T {
    return withContext(coroutineContext) {
        this@transactionWithContextResult.transactionWithResult(noEnclosing) { body }
    }
}
Using in DatabaseHelper:
Copy code
suspend fun getAllItems(): List<Item> {
    dbRef.transactionWithContextResult<List<Item>>(backgroundDispatcher) {
        dbQueries.getAllItems(::mapItemsSelecting).executeAsList()
    }
}
r
I guess it’s a better question for #squarelibraries but what exactly does not work here? The type cannot be inferred I assume?
l
message has been deleted
r
you have to invoke your
body
actually, so change it to either
body.invoke()
or just
body()
l
🤦‍♀️ Thanks! I'll give it a try later :)