首页 > 解决方案 > Kotlin Dispatchers 如何与非挂起函数一起工作

问题描述

我是 kotlin 协程的新手,我需要知道 kotlin 协程调度程序在特定情况下是如何工作的
例如

suspend fun executeDBTransaction(){
   withContext(Dispatchers.IO){
      executeTransactions()
  }
} 

fun executeTransactions(){
  transaction.beginTransaction()
   executeUpdateQuery() // non suspend function
   executeDeleteQuery()// non suspend function
   executeInsertQuery()// non suspend function
  transaction.successful()
}

从上面的例子中我需要知道的是

我在 executeTransactions 方法中执行的所有查询都将在单线程上运行吗?

如果我的 executeUpdateQuery 在线程 IO-1 上运行并在线程 IO-2 上运行 executeDeleteQuery,那么它会导致事务不成功。
谁能帮我这个?

标签: androidkotlintransactionskotlin-coroutines

解决方案


推荐阅读