【问题标题】:Firestore updating 2 different documentsFirestore 更新 2 个不同的文档
【发布时间】:2019-02-20 20:01:40
【问题描述】:

我在我的应用程序中使用 Cloud Firestore,并且有 2 个集合 Customers 和 Properties。我有一个活动,用户可以更新客户文档名称地址等中包含的数据。下面显示的这段代码工作正常。

db.collection("Customers").document(customer.getCustomerId())
                .update(
                        "name", c.getName(),
                        "email", c.getEmail(),
                        "phoneNo", c.getPhoneNo(),
                        "address", c.getAddress(),
                        "creatorId", c.getCreatorId()
                )
                .addOnCompleteListener(new OnCompleteListener<Void>() {

我在属性文档中保存了客户的文档参考,因此我可以参考哪个客户拥有哪个属性。使用此参考,我想搜索包含该参考的属性,并在名称字段已更改时更新它。在 onComplete 检查上述代码之后,我尝试将代码添加到我的方法中,但它不会每次仅每隔几次尝试就更新名称字段。

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
        CollectionReference propRef = rootRef.collection("Properties");
        propRef.whereEqualTo("customerId", customerId).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
                        Map<Object, String> map = new HashMap<>();
                        map.put("customer", customerName);
                        propRef.document(document.getId()).set(map, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {

有没有办法实现我想要做的事情? 我确实认为我可以使用批处理来完成,但从我读过的内容来看,这不允许搜索。

@AlexMamo

这是我的客户收藏中的文档

这是我的 Properties 集合中的链接文档

客户结构

属性结构

【问题讨论】:

  • “我可以使用批处理来完成,但根据我的阅读,这不允许搜索。”等什么?批处理写入只是一组写入操作,如果您想同时更新多个文档并处理数据一致性,可以运行批处理。
  • 我不知道如何搜索匹配的 Properties 文档并在批量写入中分配更改
  • 请为这两个集合添加您的数据库结构,也请回复@AlexMamo
  • @AlexMamo 我添加了 2 个客户的屏幕截图和链接的属性条目
  • 我在您的属性对象中看不到任何文档引用。你在说什么参考,DocumentReference?您能否解释一下您首先要执行的查询是什么,以及基于该查询的第二个是什么?根据您的示例,预期结果是什么?

标签: java android firebase google-cloud-firestore


【解决方案1】:

根据您的cmets,为解决您的问题,请使用以下代码行:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference propertiesRef = rootRef.collection("Properties");
CollectionReference customersRef = rootRef.collection("Customers");
customersRef.whereEqualTo("customerId", customerId).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                String customerName = document.getString("name");
                propertiesRef.whereEqualTo("customerId", customerId).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot doc : task.getResult()) {
                                propertiesRef.document(doc.getId()).update("customer", customerName);       
                            }
                        }
                    }
                });
            }
        }
    }
});

您应该使用不同的CollectionReference 对象,propertiesRef 和customersRef,您使用的是单个对象。

【讨论】:

  • 谢谢,这正是我在完美地交换参考调用后所需要的,谢谢
【解决方案2】:

您需要从 firestore OnDataChange 实现接口并接收绑定到视图的变量的新值。

在这种情况下,当您修改或更新 Firestore 中的值时,更改会触发此接口,您可以分配新的更改。

ValueEventListener myListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        Customer customer = dataSnapshot.getValue(Customer.class);
        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
    }
};
rootRef.addValueEventListener(myListener);

希望它对你有用。

https://firebase.google.com/docs/database/android/read-and-write?authuser=0

【讨论】:

  • 谢谢,如果客户文档更新,我可以使用 onDataChange 更新匹配的属性文档?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-11
  • 2023-04-05
  • 1970-01-01
  • 2019-07-20
  • 2021-01-21
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多