Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我需要在Firestore中更新地图的某个数组条目_Android_Firebase_Kotlin_Google Cloud Firestore - Fatal编程技术网

Android 我需要在Firestore中更新地图的某个数组条目

Android 我需要在Firestore中更新地图的某个数组条目,android,firebase,kotlin,google-cloud-firestore,Android,Firebase,Kotlin,Google Cloud Firestore,我想更新我的产品阵列的一个产品 我收集了一系列产品 products[{ - 0 productPrice: 123 - 1 productPrice: 432 }] 在我的代码中,我已经传递了我想要在产品数组中更新的元素的位置 suspend fun updateProductPrice(position:Int,shopId: String,price: Int): Resource<Unit> { FirebaseFirestore

我想更新我的产品阵列的一个产品

我收集了一系列产品

products[{
   - 0
   productPrice: 123
   - 1
   productPrice: 432
}]
在我的代码中,我已经传递了我想要在产品数组中更新的元素的位置

  suspend fun updateProductPrice(position:Int,shopId: String,price: Int): Resource<Unit> {
        FirebaseFirestore.getInstance().collection("shops").document(shopId).update("products"[position]) //here I need to get productPrice and update its value to the price parameter in my method
        return Resource.Success(Unit)
    }
suspend fun updateProductPrice(位置:Int,shopId:String,价格:Int):资源{
FirebaseFirestore.getInstance().collection(“shops”).document(shopId).update(“products”[position])//这里我需要获取productPrice并将其值更新为方法中的price参数
返回资源。成功(单位)
}
我需要用该数组来更新productPrice(在位置0处)值,但我找不到如何更新


谢谢

不可能通过数组的索引来更新数组。您必须读取文档,以您想要的方式修改内存中的数组,然后将数组字段更新回文档。如果您需要原子操作,可以使用事务。

我已经解决了我的问题,正如doug所说,我已经编辑了数组中需要更改价格的元素,然后我只是用所有数组更新整个数据数组,但在我所需位置的一个条目更新后,我就这样做了

 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)
    }
suspend-fun-updateProductPrice(位置:Int,shopId:String,价格:Int,产品列表:可变列表):资源{
productList[位置]。价格=价格
FirebaseFirestore.getInstance().collection(“shops”).document(shopId).update(“products”,productList).Wait()
返回资源。成功(单位)
}

谢谢doug,我正在考虑获取所有数组,只更新一个元素,然后在同一个数组节点中再次设置所有数组,但现在使用更新的数组字段,可以吗?