如何处理多个相互依赖的房间查询?

问题描述

我想通过多个数据库查询数据库添加值。 这些操作不需要 UI 的东西,所以我认为还有另一种方法可以实现这一点。我知道 有事件通道,我可以在每个房间查询后发送在活动/片段中收集的密封类, 但我认为应该有更简单的方法

我做了一个草稿(只是一段理论上的代码): 在 viewmodel 中,我有一个这样的函数,其中 5 个 DB 查询将结果相互连接:

fun add(item: Something) = viewmodelScope.launch {

  val itemsNumber = itemDao.getItems()
  val list = mutablelistof()

  if (itemsNumber == 0){
    val newItem = Item()
    val insertedId = itemDao.insert(newItem)

    val other = Other(id = insertedId,title = "Sha-la-la")
    val insertedOtherId = otherDao.insert(other)

    list = itemDao.getItems()
    if (list.size > 0){
       val anotherItemList = mutablelistof(Another(oid = insertedOtherId),Another(oid = insertedOtherId) )
       val insId = anotherDao.insertAll(anotherItemList)
    }
}

   

项目 DAO:

@Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(item: Item): Long

@Transaction
@Query("SELECT COUNT(*) FROM items WHERE id = :itemId ")
suspend fun getItems(itemId: Long): Int

其他道:

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(other: Other): Long

一个道:

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(item: List<Another>): List<Long>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...