【发布时间】:2020-04-18 23:31:49
【问题描述】:
我的计划是不删除数组中已经存在的值,我有这个数组
products [{
- 0
productPrice : 123
productName: 32
- 1
productPrice : 432
productName: 21
]}
现在,我更新数组中元素 0 的价格,以便获取整个数组,编辑价格,然后将数组发送到相同的节点产品,但是当我通过更新执行此操作时,我期待只需触摸并更新价格值,而是 .update 正在删除我的 productName 字段
suspend fun updateProductPrice(position:Int,shopId: String,price: Int,productList:MutableList<Product>): Resource<Unit> {
productList[position].price = price
FirebaseFirestore.getInstance().collection("shops").document(shopId).update("products",productList).await()
return Resource.Success(Unit)
}
我的产品数据类
data class Products(val productPrice:Int = 0)
现在,我知道我这里没有 productName,但是如果我要更新的整个数组中唯一的东西是价格,我就不需要它,那么,为什么我的更新会删除 productName 如果我告诉我只更新价格值?
另外,我知道添加 productName:String = "" 不会删除它并保持原样,但我的用例需要只触摸该价格元素而不是其他元素,因为我的产品中有更多元素数据类,但如果我也将它们放置在整个文档中,它还会将这些字段添加到我不想要的数组中,因为它会使用我不想更新的默认字段值增加文档大小
有什么提示吗?
【问题讨论】:
标签: android firebase kotlin google-cloud-firestore