Ami*_*Sen 3 android firebase google-cloud-firestore
使用 Cloud Firestore 时,最好记住阅读的文档计数。我正在开发一个库存应用程序,其中为产品列表以及其他详细信息生成账单。除了账单中数组列表的更新之外,一切正常。我可以在账单中插入产品的数组列表以及其他详细信息,但稍后无法更新数组列表。每当我尝试在文档中插入更多数组列表时,旧的数组列表就会被删除。我不明白如何在该数组列表中插入更多条目。
private void loadDatatoFirestore() {
//product details entry
final ArrayList<OrderedProductModel> newProductModel = new ArrayList<>();
for (int i=0; i<productModelList.size(); i++){
OrderedProductModel model = new OrderedProductModel(productModelList.get(i).getProductNumber(),
productModelList.get(i).getProductName(),
productModelList.get(i).getOrderedQuantity(),
productModelList.get(i).getSellingPrice());
newProductModel.add(model);
}
BillModel insertBill = new BillModel(billNumber, partyName, date, totalPrice, BILL_STATUS, newProductModel);
db.collection("mainCollection").document("BillDocument")
.collection("BillCollection")
.document(billNumber)
.set(insertBill, SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener<Void>() {...}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常..但现在我想将更多条目插入 orderderProduct 数组。我试图像这样解决它,但随后发生编译时错误。我不知道如何在这里更新数组。
private void updateBill(String billNumber) {
//here i am trying to insert dummy data
ArrayList<OrderedProductModel> testArray = new ArrayList<>();
testArray.add(new OrderedProductModel("9999","testprod1",50,40));
testArray.add(new OrderedProductModel("9911","testprod2",70,60));
BillModel billtest = new BillModel(testArray);
db.collection("mainCollection").document("BillDocument")
.collection("BillCollection")
.document(billNumber)
.update(billtest,SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener<Void>() {....}
Run Code Online (Sandbox Code Playgroud)
比尔模型
public class BillModel {
String billNumber;
String partyName;
String billingDate;
float totalPrice;
String status;
ArrayList<OrderedProductModel> orderderProduct;
public BillModel(ArrayList<OrderedProductModel> orderderProduct) {
this.orderderProduct = orderderProduct;
}
public BillModel(String billNumber, String partyName, String billingDate, float totalPrice, String status, ArrayList<OrderedProductModel> orderderProduct) {
this.billNumber = billNumber;
this.partyName = partyName;
this.billingDate = billingDate;
this.totalPrice = totalPrice;
this.status = status;
this.orderderProduct = orderderProduct;
}
Run Code Online (Sandbox Code Playgroud)
订购产品型号
public class OrderedProductModel {
private String productNumber;
private String productName;
private int orderedQuantity;
private float sellingPrice;
public OrderedProductModel(String productNumber, String productName, int orderedQuantity, float sellingPrice) {
this.productNumber = productNumber;
this.productName = productName;
this.orderedQuantity = orderedQuantity;
this.sellingPrice = sellingPrice;
}
Run Code Online (Sandbox Code Playgroud)
附加图像...
firestore 图像
账单创建图像
如果要更新 Firestore 中数组类型的内容,则不能简单地合并到新数组中。在合并期间,新字段值将始终完全替换旧字段值。
如果要修改数组的内容,则必须使用特殊的FieldValue.arrayUnion()操作作为字段的值。
根据 API 文档:
返回一个可以与 set() 或 update() 一起使用的特殊值,告诉服务器将给定元素与服务器上已存在的任何数组值联合起来。数组中尚不存在的每个指定元素都将添加到末尾。如果正在修改的字段还不是数组,它将被包含确切指定元素的数组覆盖。
不幸的是,这与 Java POJO 对象中发生的自动字段映射不兼容,就像您使用 BillModel 一样。您必须转换代码才能使用 Map 类型对象更新文档,手动将要修改的每个 BillModel 字段的值复制到其中。
| 归档时间: |
|
| 查看次数: |
5018 次 |
| 最近记录: |