【问题标题】:Unlike Livedata, using Flow in Room query doesn't trigger a refresh when updating a table entry but works if I delete or insert an entry与 Livedata 不同,使用 Flow in Room 查询在更新表条目时不会触发刷新,但在我删除或插入条目时有效
【发布时间】:2021-05-17 15:04:27
【问题描述】:

当使用 Livedata 作为对 Room 中表的 select* 查询的返回类型时,我观察它,如果我更新/插入/删除该表中的条目,我会得到触发器。但是,当我尝试使用 Kotlin Flow 时,我只得到了 2 个触发器。

第一个触发器给出一个空值,因为状态流的初始值为空。第二个触发器是 Room 表中的条目列表。

如果我对数据库执行插入/删除操作,我会收到来自 StateFlow 的触发器。 但是,如果我更新条目,Stateflow 不会触发。

注意:更新操作在数据库上正常工作。我使用 DB 检查器进行了检查。

数据类和DAO

@Entity
data class CartItem (
    @PrimaryKey
    val itemId: Int,
    var itemQuantity: Int=1
)

@Dao
interface CartDao {

    @Query("SELECT * FROM CartItem")
    fun getAllItems(): Flow<List<CartItem>>

    @Update
    suspend fun changeQuantityInCart(cartItem:CartItem)

    @Insert
    suspend fun insert(item: CartItem)

    @Delete
    suspend fun delete(cartItem:CartItem)
}

视图模型

val cartItems: StateFlow<List<CartItem>?> =
        repo.fetchCartItems().stateIn(viewModelScope, SharingStarted.Lazily, null)

片段

viewLifecycleOwner.lifecycleScope.launchWhenStarted {
            viewModel.cartItems.collect {
              Log.e("Update","Update")
 }

【问题讨论】:

    标签: android android-room kotlin-flow kotlin-stateflow


    【解决方案1】:

    我的陷阱是我像这样更新对象:

     currentItem.itemQuantity = currentItem.itemQuantity + 1
     changeQuantity(currentItem)
    

    currentItemCartItem 类的对象,最初从 DAO 中的 getAllItems 流接收。)

    (changeQuantityfun 在 DAO 中调用 changeQuantityInCartfun。


    这导致 StateFlow 中 CartItem 对象的引用在调用 DB 上的更新之前使用新的 itemQuantity 值保存对象的更新值。

    之后,在 DAO 中调用 Update fun 时,DB 条目会更新,Flow 值会发生变化,但将其放入 Stateflow 时不会检测到任何变化。因此,状态流不会触发,因为它是状态流与实时数据的不同之处。

    在livedata的情况下,无论新值是否相同都会触发。

    因此,要解决此错误,请不要在调用数据库更新操作之前更改 stateFlow 中对象的值,如下所示:

    val updatedCartItem = cartItem.copy(itemQuantity = cartItem.itemQuantity + 1)
    changeQuantity(updatedCartItem)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 2020-09-02
      • 2011-07-04
      • 2020-12-21
      • 1970-01-01
      相关资源
      最近更新 更多