简体   繁体   English

Android Room 在继续下一行之前是否等待 withTransaction 块完成

[英]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.

相关问题 Android如何在继续下一个代码之前等待onComplete方法结束 - Android how to wait for onComplete method to end before proceeding to the next code 等待几个流程完成后再继续 - Wait for several Flows to finish before proceeding 等待Asynctasks完成,然后再进行新活动 - Wait Asynctasks to finish before proceeding to new activity 等待数据库完成插入数据,然后继续下一个活动 Android - Wait for Database to finish inserting data before continuing to next activity Android 在运行到android中的下一条语句之前等待循环完成服务器调用 - Wait for loop to finish server call before running to next statement in android 在进行方法之前获取排球请求完成的价值? Android Java - Get value for volley request to finish before proceeding with method? Android Java 如何使用 Mockk 模拟 android 房间 withTransaction 方法 - How to mock android room withTransaction method with Mockk Android studio,等一行代码完成再开始下一步 - Android studio, wait for one line of code to complete before starting next Android-等待动画完成再继续吗? - Android - wait for animation to finish before continuing? Android:如何在继续之前等待方法完成 - Android : how to wait for a method to finish before continuing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM