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 }