Erbez
Erbez

Reputation: 321

Method always returns false from Firestore DB query

I have a method that checks if a list contains a user or not. For some reason it always returns false, even though the user is in the list. The function does work, I know it does find the user, just not sure why it doesn't return anything else but false.

I know it works because I have another method with this code snippet in to check if the user is in the list and remove or add them. That works so I know it is pulling the list.

Method:

fun checkUserChatChannel(channelId: String): Boolean {
    var list = mutableListOf<String>()
    val currentUserId = FirebaseAuth.getInstance().currentUser!!.uid
    var bool = false
    chatChannelsCollectionRef.document(channelId).get().addOnSuccessListener {
        if (it.exists()) {
            list = it.get("userIds") as MutableList<String>
            if(list.contains(currentUserId)){
                bool = true
            }
        }
    }
    return bool
}

Calling:

boolean inOut = FirestoreUtil.INSTANCE.checkUserChatChannel(moduleId);
if(!inOut)
{
    JLChat.setText("Join");
}
else
{
    JLChat.setText("Leave");
}

But the button will always remain on "Join":

This is the other method that I use so I know the code works to remove a user from the list(Nothing to do with question):

fun LeaveGroup(channelId: String) {
    currentUserDocRef.collection("engagedChatChannels").document(channelId)
            .delete()
    var list = mutableListOf<String>()
    val currentUserId = FirebaseAuth.getInstance().currentUser!!.uid
    chatChannelsCollectionRef.document(channelId).get().addOnSuccessListener {
        if (it.exists()) {
            list = it.get("userIds") as MutableList<String>
            if(list.contains(currentUserId)){
                list.remove(currentUserId)
            }
        }
        val newChannel = chatChannelsCollectionRef.document(channelId)
        newChannel.set(GChatChannel(channelId,list))
    }
}

Upvotes: 1

Views: 730

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 139029

Firebase APIs are asynchronous, meaning that onSucces() function returns immediately after it's invoked, and the callback from the Task it returns, will be called some time later. There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available. Because that method returns immediately, the value of your bool variable you're trying to return, will not have been populated from the callback yet.

Basically, you're trying to return a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.

A quick solve for this problem would be to use the value of your bool variable only inside the callback. This also means that you need to change your checkUserChatChannel instead of returning a boolean to return void, otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback.

Upvotes: 3

Related Questions