Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - Sometimes value is not saving

Take a look at this GIF I recorded:

(GIF

This is the database of a counter app I'm making. When a user increments, count gets added along with the count under

users:{
   UID:{
     count: x
   }
}

However, if you can notice in the GIF, sometimes, the upper count gets incremented but the one under users doesn't. Here's the code I'm saving it with:

database = database.child("users").child(auth.getCurrentUser().getUid()).child("count");
    final DatabaseReference finalDatabase = database;
database.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Get the latest int
            final int[] i = {Integer.parseInt(button.getText().toString())};
            //add to the user's count with an onFailureListener
            finalDatabase.setValue(i[0]++).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e(TAG,"Adding user's count failed: " + e.getMessage());
                }
            }).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    //Now add to the original
                    DatabaseReference database = FirebaseDatabase.getInstance().getReference();
                    database.child("Campaigns").child(key).child("count").setValue(i[0]++).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.e(TAG, "Error: " + e.getMessage());
                        }
                    });
                }
            });

None of the onFailure methods get called. So why is this happening?

like image 561
Ali Bdeir Avatar asked Nov 08 '22 10:11

Ali Bdeir


1 Answers

You can instead listen to the users node to count the total count. Here's an example

private Map<String, Long> userCount = new HashMap<>();
private Long totalCount = 0L;

private void addTotalCountListener() {
    FirebaseDatabase.getInstance().getReference().child("users").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            User user = dataSnapshot.getValue(User.class);
            userCount.put(dataSnapshot.getKey(), user.getCount);
            totalCount += user.getCount();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            // subtract the total count with the previous count
            totalCount -= userCount.get(dataSnapshot.getKey());
            // then add the new count
            User user = dataSnapshot.getValue(User.class);
            userCount.put(dataSnapshot.getKey(), user.getCount);
            totalCount += user.getCount();
        }
    });
}
like image 70
Wilik Avatar answered Nov 14 '22 21:11

Wilik