`addListenerForSingleValueEvent()` is not being triggered in android firebase

Viewed 70

Same code would work previously, but now suddenly it stopped working without changing anything!

My code is:

private void readData(final ReadData read) {
    Log.i("test", "Users 1: " + ref);
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            users.clear();
            totalUsers.clear();
            Log.i("test", "Users 2: " + ref);
            for (DataSnapshot items : snapshot.getChildren()) {
                if (items.exists()) {
                    UserChangeInfo user = items.getValue(UserChangeInfo.class);
                    assert user != null;
                    totalUsers.add(user);
                    if (!user.getNewPassword().equals("")) {
                        users.add(user);
                    }
                }
            }
            read.onCallback();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Log.i("test", "Error 1: " + error.getMessage());
            Log.i("test", "Error 2: " + error.getDetails());
        }
    });

}

Here, Log.i("test", "Users 1: " + ref); gets executed. But Log.i("test", "Users 2: " + ref);, Log.i("test", "Error 1: " + error.getMessage());, Log.i("test", "Error 2: " + error.getDetails()); don't get executed.

So, it seems, addListenerForSingleValueEvent() doesn't get triggered. I'm stuck here for days. Why??

I read this way:

readData(new ReadData() {
            @Override
            public void onCallback() {
                Log.i("test", "Users 3: " + ref);
                adapter.notifyDataSetChanged();
                String s = "Total Users: " + totalUsers.size() + "\n\n\nUpdate Request: " + users.size();
                txtTotalChangeReq.setText(s);

                if(users.size() > 0){
                    lstUpdateRequestList.setVisibility(View.VISIBLE);
                    lstTotalUserList.setVisibility(View.GONE);
                    showRequestedUsers.setChecked(true);
                    showAllUser.setChecked(false);
                    showInactiveUsers.setChecked(false);
                    String ss = "Change request list";
                    listLabel.setText(ss);
                }
                else{
                    lstTotalUserList.setVisibility(View.VISIBLE);
                    lstUpdateRequestList.setVisibility(View.GONE);
                    showAllUser.setChecked(true);
                    showRequestedUsers.setChecked(false);
                    showInactiveUsers.setChecked(false);
                    String ss = "Total user list";
                    listLabel.setText(ss);
                }
            }
        });
        Log.i("test", "Users 4: " + ref);

But here Log.i("test", "Users 3: " + ref); is not executed also nothing here gets executed but out this call Log.i("test", "Users 4: " + ref); gets executed.

note: There is an warning as W/System: Ignoring header X-Firebase-Locale because its value was null. but everyone is saying this warning has nothing to do with my code.

N/B:

  1. Declared ref as: ref = FirebaseDatabase.getInstance().getReference().child("change request");
  2. ref is not null: ref = https://myFirebaseAppLink/change%20request
  3. Authentication is allowed.
  4. Used the dependencies: implementation platform('com.google.firebase:firebase-bom:26.1.1') and other firebase dependandencies.
  5. Wi-fi connection is Okay. (Other firebase apps is working also).
  6. App is connected with firebase.
1 Answers

I had a almost similar problem. Use a temporary ArrayList ex: tmp1,tmp2 instead of users and totalUsers, then pass these two ArrayList to read.onCallback(tmp1,tmp2).[inside private void readData method(1st code)]. Then inside your readData>onCallBack[2nd code]

users.clear();
totalUsers.clear();
users.addAll(tmp1);
totalUsers.addAll(tmp2);

then do your work.

Related