1

When i query for a single document Reference from the collection its looping through multiple times .i.e when i start querying for a document example my sub-collection name ex: "Beverages" i get the right query document details But when my sub-collection name changes ex: "Fruits" i get the result but my query is looping through multiple times . i don't know why because of shared preference(used preference to send selected collection id from main activity to this) or something else couldn't find it help needed thanks.

My code :

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();
                        }
                    })

My Log output if you see I have put a simple log where OnEvent is getting printed multiple times:

The first log where the results were as expected

 { 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 }

Second log where looped multiple times

{  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 }
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
AndroidRocket
  • 418
  • 3
  • 14
  • The error message indicates that `attachment` is `null`. There's not much we can do to help that is not already mentioned here: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Apr 13 '20 at 14:29

1 Answers1

1

But when my sub-collection name changes ex: "Fruits" I get the result but my query is looping through multiple times

I think that is the correct behavior for the firebase realtime database. Whenever you are updating an item in the firebase database, the change listener gets called to fetch the updated data from your database to be populated in your android client.

Hence, if you do not want the lastest updated to be fetched again, you can detach the listener as mentioned in the doc here.

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

Then when you are done populating your object (e.g. the cart), you can remove the listener so that you do not get any more updates from the database.

registration.remove();

Hence, when the load is finished, you may call the remove on your listener so that you stop listening to the firebase database changes.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98