I'm trying to get a value and then increase that value by one. The problem is I cannot get this value out of onDataChange method, if I do the job inside the method I get loop and it continuously adding ones to this value. What should I do?
Here is my code:
rootRef.child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int rat = Integer.parseInt(dataSnapshot.child(current_user).child("rating").getValue().toString());
System.out.println(rat);
rat = rat + 1;
rootRef.child("users").child(current_user).child("rating").setValue(rat);
System.out.println(rat);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});