我无法从Firebase数据库中检索布尔数据

时间:2017-05-17 02:52:11

标签: android firebase firebase-realtime-database

我正在尝试创建一个Android聊天应用程序。在这种情况下,我试图让人们互相添加朋友(经典)。但我无法从Firebase数据库中检索布尔数据。它总是返回false /可能为null。检查我的Firebase后端;

friends{
 552nQquUrtSgbVVLgXxfxOh6SL52{
      NuJohj7nalWnpiMfC1zl3r77QDt2: true
      vIYkKWAWwKTRD4NQfixzcNnfk6X2: true
   }
}     

我的Android代码如下;

 public boolean checkFriend(final String uID){
    mDatabase = FirebaseDatabase.getInstance().getReference("friends");
    mDatabase.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
           ret= (Boolean) snapshot.child(uID).getValue();               
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.i("HATA OLUŞTU!","FriendManager checkFriend'de veri tabanı bağlantısı hatası oluştu."+ databaseError);
        }
    });
    Log.i("Database", mDatabase.child(user.getUid()).toString()+" +++ "+uID+" ++++ "+ ret.toString());
    return ret;
}

我的logcat在

之下
05-17 03:40:35.803 22488-22488/com.furkan.profil I/Database: https://profil-95859.firebaseio.com/friends/552nQquUrtSgbVVLgXxfxOh6SL52 +++ 539vt6Cwi0Y4dF3qmgI6LN1kJJo1 ++++ false
05-17 03:40:35.805 22488-22488/com.furkan.profil I/Database: https://profil-95859.firebaseio.com/friends/552nQquUrtSgbVVLgXxfxOh6SL52 +++ NuJohj7nalWnpiMfC1zl3r77QDt2 ++++ false
05-17 03:40:35.805 22488-22488/com.furkan.profil I/Database: https://profil-95859.firebaseio.com/friends/552nQquUrtSgbVVLgXxfxOh6SL52 +++ vIYkKWAWwKTRD4NQfixzcNnfk6X2 ++++ false

`

2 个答案:

答案 0 :(得分:2)

return ret将在onDataChange之前运行。 return ret不要等到onDataChange()onCancelled()完成,因为它们在不同的线程中运行

mDatabase.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        // this will run in different thread 
        ret = (Boolean) snapshot.child(uID).getValue(); // the value that from Firebase is here
    }
});
// this will run before onDataChange
return ret;   

所以你需要改变你的功能

public void checkFriendThenDoSomeThing(final String uID){
    mDatabase.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
           ret= (Boolean) snapshot.child(uID).getValue(); 

           // DO SOMETHING after get ret from Firebase
        }
        ...
    });
}

<强> ==
或者如果您想要来自其他checkFriend(...)的来电 Activity,您需要:
首先,声明Listener

的接口
public interface CheckFriendSuccessListener {
    void onSuccess(boolean value);
}

Listener放入您的功能

public void checkFriend(final String uID, CheckFriendSuccessListener onCheckFriendSuccessListener){
    mDatabase.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            boolean ret= (Boolean) snapshot.child(uID).getValue();
            onCheckFriendSuccessListener.onSuccess(ret);
        }
    });
}

然后你打电话给checkFriend

checkFriend("someUserID", new CheckFriendSuccessListener() {
    @Override
    public void onSuccess(boolean value) {
         // DO SOMETHING after check friend successful
    }
});

答案 1 :(得分:0)

尝试在ret方法中返回onDataChange,如下所示:

@Override
public void onDataChange(DataSnapshot snapshot) {
    ret= (Boolean) snapshot.child(uID).getValue();
    Log.i("Database", mDatabase.child(user.getUid()).toString()+" +++ "+uID+" ++++ "+ ret.toString());
    doStuff(ret); // user ret here
}