【问题标题】:Firestore Query is looping through multiple timesFirestore Query 循环多次
【发布时间】:2020-07-26 00:24:10
【问题描述】:

当我从集合中查询单个文档引用时,它会循环多次。即当我开始查询文档示例时,我的子集合名称例如:“饮料”我得到了正确的查询文档详细信息但是当我的子- 集合名称更改例如:“水果”我得到结果,但我的查询循环多次。我不知道为什么因为共享偏好(使用偏好将选定的集合 id 从主要活动发送到此)或其他无法找到需要帮助的东西,谢谢。

我的代码:

private void GettingQuantity() {

    Log.d(TAG, "GettingQuantity: Bstarted");

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String MainCatId = preferences.getString("Main_Id",null);
    String MainCatTittle = preferences.getString("Main_tittle",null);
    String SubCatId = preferences.getString("Sub_Main_Id",null);
    String SubCatTittle = preferences.getString("Sub_Main_tittle",null);
    final String item_id = preferences.getString("ItemID",null);
    final int oldValue = preferences.getInt("OldValue",0);
    final int newValue = preferences.getInt("NewValue",0);



    //sharing to seperate cart node in store
    FirebaseFirestore dQ = FirebaseFirestore.getInstance();
    DocumentReference ProductRef = dQ.collection("HomeFeed")
            .document("5HEkE0ac7sMa7Gjnvf3E")
            .collection("MainCategory")
            .document(MainCatId)
            .collection(MainCatTittle)
            .document(SubCatId)
            .collection(SubCatTittle)
            .document(item_id);

    ProductRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
            Attachment attachment = documentSnapshot.toObject(Attachment.class);
            Log.d(TAG, "onEvent: Bstarted");

            if (newValue == 1 && oldValue == 0) {

                FirebaseFirestore db = FirebaseFirestore.getInstance();
                CollectionReference CartRef = db.collection("Cart");
                Attachment cartItem = new Attachment();

                String cart_id;
                cart_id = item_id;
                cartItem.setItem_id(cart_id);
                cartItem.setItem_brand(attachment.getItem_brand());
                cartItem.setItem_name(attachment.getItem_name());
                cartItem.setItem_price(attachment.getItem_price());
                cartItem.setQuantity(attachment.getQuantity());
                cartItem.setItem_discount(attachment.getItem_discount());
                cartItem.setItem_quantity(newValue);
                cartItem.setUrls(attachment.getUrls());
                Log.d(TAG, "passData: the cart Id: " + cart_id);

                    CartRef.document(cart_id).set(cartItem).addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Toast.makeText(mContext, "Added to cart", Toast.LENGTH_SHORT).show();
                        }
                    })

我的日志输出,如果您看到我放置了一个简单的日志,其中 OnEvent 被多次打印:

结果符合预期的第一个日志

 { 2020-04-13 23:40:08.039 17940-17940/com.example.myconsumer D/ProductItemsActivity: onReceive: Bstarted

  2020-04-13 23:40:08.039 17940-17940/com.example.myconsumer D/ProductItemsActivity: GettingQuantity: Bstarted

  2020-04-13 23:40:08.063 17940-17940/com.example.myconsumer D/ProductItemsActivity: onEvent: Bstarted }

循环多次的第二个日志

{  2020-04-13 23:40:55.062 17940-17940/com.example.myconsumer D/ProductItemsActivity: onReceive: Bstarted

  2020-04-13 23:40:55.062 17940-17940/com.example.myconsumer D/ProductItemsActivity: GettingQuantity: Bstarted

  2020-04-13 23:40:55.064 17940-17940/com.example.myconsumer D/ProductItemsActivity: onReceive: Bstarted

  2020-04-13 23:40:55.064 17940-17940/com.example.myconsumer D/ProductItemsActivity: GettingQuantity: Bstarted

  2020-04-13 23:40:55.076 17940-17940/com.example.myconsumer D/ProductItemsActivity: onEvent: Bstarted

  2020-04-13 23:40:55.083 17940-17940/com.example.myconsumer D/ProductItemsActivity: onEvent: Bstarted }

【问题讨论】:

标签: java android firebase google-cloud-firestore


【解决方案1】:

但是当我的子集合名称更改例如:“水果”时,我得到了结果 但我的查询循环多次

我认为这是 firebase 实时数据库的正确行为。每当您更新 firebase 数据库中的项目时,都会调用更改侦听器以从数据库中获取更新的数据以填充到您的 android 客户端中。

因此,如果您不希望再次获取最新更新,您可以分离侦听器 as mentioned in the doc here

// Assign the listener to a variable first
ListenerRegistration registration = ProductRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
    // ... Your onEvent code
}

然后,当您完成填充对象(例如购物车)后,您可以删除侦听器,这样您就不会从数据库中获得更多更新。

registration.remove();

因此,加载完成后,您可以在侦听器上调用 remove,以便停止侦听 firebase 数据库更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多