user5673373
user5673373

Reputation:

I can't retrieve boolean data from Firebase Database

I am trying to create an Android chat application. At this context, I am trying to let people add friend each other(a classic). But I can't retrieve boolean data from Firebase Database. It is always returning false/maybe null. Check my Firebase backend;

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

And my Android code is below;

 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;
}

And my logcat is below

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

`

Upvotes: 1

Views: 3271

Answers (2)

Linh
Linh

Reputation: 60973

return ret will run before onDataChange. return ret don't wait until onDataChange() or onCancelled() finished because they run in different thread

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;   

So you need to change your funtion to

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
        }
        ...
    });
}

======
OR If you want call checkFriend(...) from another Activity, your need to:
First, declare a interface for Listener

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

Put the Listener to your function like

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);
        }
    });
}

then when you call the checkFriend

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

Upvotes: 1

Atu
Atu

Reputation: 937

Try returning the ret inside the onDataChange method like this:

@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
}

Upvotes: 0

Related Questions