Suraj Giri
Suraj Giri

Reputation: 162

Firebase keeps changing the value and adding new child

I am trying to load data from firebase database and then I want to add some more data to it and in the end I want it to upload back. But the problem is when the data is being uploaded back it is not stopping after updating the data. It looks like it is under loop and doing the task again and again.

mTempDatabase.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if(dataSnapshot.hasChild(mAuth.getCurrentUser().getUid())){
                                final DatabaseReference newTemp = mTempDatabase.child(mAuth.getCurrentUser().getUid());
                                final long qntCount = (long) dataSnapshot.child(mAuth.getCurrentUser().getUid()).child("QuantityCount").getValue();
                                final long foodamount = (long) dataSnapshot.child(mAuth.getCurrentUser().getUid()).child("QuantityCount").getValue();
                                //final long countfood = (long) dataSnapshot.child(mAuth.getCurrentUser().getUid()).child("QuantityCount").getValue();
                                mDatabaseBestseller.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        String foodname = (String) dataSnapshot.child(food_key).child("foodname").getValue();
                                        long foodprice = (long) dataSnapshot.child(food_key).child("foodprice").getValue();
                                        long mfoodprice = foodprice + foodamount;
                                        long mqntCOunt = qntCount +1;
                                        newTemp.child("QuantityCount").setValue(mqntCOunt);
                                        newTemp.child("FoodPrice1").setValue(mfoodprice);

                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });

                            }

                        }


                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                    mTempDatabase.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if(!dataSnapshot.hasChild(mAuth.getCurrentUser().getUid())) {
                                final DatabaseReference newTemp = mTempDatabase.child(mAuth.getCurrentUser().getUid());
                                mDatabaseBestseller.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        String foodname = (String) dataSnapshot.child(food_key).child("foodname").getValue();
                                        long foodprice = (long) dataSnapshot.child(food_key).child("foodprice").getValue();

                                        newTemp.child("QuantityCount").setValue(1);
                                        newTemp.child("CountFood1").setValue(1);
                                        newTemp.child("Food1").setValue(foodname);
                                        newTemp.child("FoodPrice1").setValue(foodprice);
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                    viewHolder.mFoodAddLayout.setVisibility(View.INVISIBLE);
                    viewHolder.mIncrLayout.setVisibility(View.VISIBLE);

                }
            });

According to my understanding I have did wrong math while retrieving the data and adding the data back.

Upvotes: 0

Views: 918

Answers (2)

Benjith Mathew
Benjith Mathew

Reputation: 1221

addValueEventListener() will always listening to database reference value changes.if you want to stop listening you have to use removeEventListener(valueListener), But addListenerForSingleValueEvent() executes onDataChange method immediately and after executing that method once, it stops listening to value changes.

Upvotes: 1

user8890895
user8890895

Reputation:

You need to use the .addListenerForSingleValueEvent() instead of .addValueEventListener(). The difference is, the second one keep updating every time a value is changed in the database, which keep happening as you update your value inside the function. But, the first method that I am suggesting, just read the values once, which is what you need here.

Upvotes: 0

Related Questions