Adrixb
Adrixb

Reputation: 153

Problem gathering data from Firebase RealtimeDatabase (Android, Java)

I'm having an issue while gathering data from a node on Firebase. I got a variable named stockBuy, I have run my app on debug mode and it gets the value correctly but out of onDataChange() my variable becomes to be 1 instead of the value from firebase + 1 as I have on this line stockBuy = stockBuy + 1;

Here you can see my code

private void deleteProduct(final int position){
    mDataBase3 = FirebaseDatabase.getInstance().getReference().child("Products");
    mDataBase4 = FirebaseDatabase.getInstance().getReference().child("Wishes");
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Products").child(MainActivity.carrito.get(position).getId());

    ref.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            stockBuy = Integer.parseInt(dataSnapshot.child("stock").getValue().toString());          
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });

    stockBuy = stockBuy + 1;

    mDataBase3.child(MainActivity.carrito.get(position).getId()).child("stock").setValue(stockBuy);
    mDataBase4.child(userUid).child(MainActivity.carrito.get(position).getId()).child("stock").setValue(stockBuy);

    MainActivity.carrito.remove(position);

    this.cAdapter.notifyDataSetChanged();

}

Upvotes: 1

Views: 40

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598827

Data is loaded from Firebase asynchronously, which means that onDataChange may execute much later than you expect. If you place breakpoints or add some logging, you can see that stockBuy = stockBuy + 1; runs before stockBuy = Integer.parseInt is ever called, which explains the problem.

Any code that needs the data from the database needs to be inside onDataChange or be called from there. So the simplest fix is:

private void deleteProduct(final int position){
    mDataBase3 = FirebaseDatabase.getInstance().getReference().child("Products");
    mDataBase4 = FirebaseDatabase.getInstance().getReference().child("Wishes");
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Products").child(MainActivity.carrito.get(position).getId());

    ref.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            stockBuy = Integer.parseInt(dataSnapshot.child("stock").getValue().toString());          

            stockBuy = stockBuy + 1;

            mDataBase3.child(MainActivity.carrito.get(position).getId()).child("stock").setValue(stockBuy);
            mDataBase4.child(userUid).child(MainActivity.carrito.get(position).getId()).child("stock").setValue(stockBuy);

            MainActivity.carrito.remove(position);

            this.cAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            throw error.toException();
        }
    });
}

Also see:

Upvotes: 2

Related Questions