[英]Does Android Room wait for withTransaction block to finish before proceeding to next line
class MyViewModel(private val db: AppDatabase): ViewModel() {
suspend fun insertAndRead(pages: List<Page>) {
db.withTransaction {
db.pageDao().insertAll(pages)
}
val pagesInserted = db.pageDao().selectAll()
}
}
I'm curious about the Room handles the withTransaction
block.我很好奇 Room 处理withTransaction
块。
Let's say with the code example above.让我们说上面的代码示例。 Does Room wait for the withTransaction
block to finish running, then go to then next line? Room 是否等待withTransaction
块完成运行,然后转到下一行?
Or it simply runs the withTransaction
block inside a coroutine, then starting the next line immediately without waiting for the withTransaction
block to complete?或者它只是在协程中运行withTransaction
块,然后立即开始下一行而不等待withTransaction
块完成?
withTransaction()
is a suspend function. withTransaction()
是一个挂起函数。 Suspend functions generally wait by suspending, they are synchronous to their callers, they don't schedule asynchronous operations.挂起函数通常通过挂起等待,它们与调用者同步,它们不调度异步操作。
So the answer is: yes, it waits for the inner block to finish executing.所以答案是:是的,它等待内部块完成执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.