Firebase Android addListenerForSingleValueEvent sometimes not returning data

家住魔仙堡 提交于 2019-12-30 17:17:28

问题


When my application starts, I check the current user's details in my Firebase database (I'm storing it's uid for that).

I'm attaching addListenerForSingleValueEvent to user's ref to read it's data.

My problem is that sometimes, it doesn't return any value, neither success nor failure.

Only clearing application's data solves it, but of course forces the user to login again.

I've read some posts at SO, but didn't find any solution.

Here's my piece of code:

DatabaseReference newUser =    
FirebaseDatabase.getInstance().getReference("users/"+uid);
newUser.addListenerForSingleValueEvent(new ValueEventListener()     
    @Override
    public void onDataChange(DataSnapshot snapshot) {}

    @Override
    public void onCancelled(DatabaseError databaseError) {}
});

回答1:


The recommendation I've seen for this is to call keepSynced(true) on the Query object.




回答2:


Please try like this. If you can't solver by this, send share me source code for this part. ...

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()){
            final HashMap<String, Object> dataMap = (HashMap<String, Object>) dataSnapshot.getValue();
            for (String key : dataMap.keySet()){
                Object data = dataMap.get(key);
                try{
                    HashMap<String, Object> moneyData = (HashMap<String, Object>) data;
                    Money money = new Money(("", "");
                }catch (ClassCastException cce){
                    try{
                        name = String.valueOf(dataMap.get("username"));
                        email = String.valueOf(dataMap.get("useremail"));
                    }catch (ClassCastException cce2){
                    }
                }
            }                            
        // do something
        }else {
        // when empty data
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    }
});



回答3:


My guess is user/:id does not exist. Double-check your error output:

DatabaseReference newUser = FirebaseDatabase.getInstance()
    .getReference("users")
    .child(uid)
    .addListenerForSingleValueEvent(new ValueEventListener()     
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            if (snapshot.exists()) {
                Log.i(TAG, snapshot.val());
            } else {
                Log.e(TAG, "Not found: " + uid);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e(TAG, databaseError.toString();
        }
    });



回答4:


I've been having the same problem for days and I finally fixed it.

Looks like my Api key was changed! And that's what was preventing addListenerForSingleValueEvent() from returning!
Go to Firebase Console, open your project, click the settings icon and go to Project settings. There you'll see Web API Key. Take that and copy it to your google-services.json file in your project's directory. Here's a snippet from that file:

  ...
  ],
  "api_key": [
    {
      "current_key": "<COPY Web Api Key HERE>"
    }
  ],
  ...

That's it. After replacing the value of "current_key" with the key from Firebase, it returned to work (previously it had some other key I think I generated at Google Console and it worked for months. Just a few days ago it stopped working)



来源:https://stackoverflow.com/questions/37703218/firebase-android-addlistenerforsinglevalueevent-sometimes-not-returning-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!