Mothod始终从Firestore数据库查询返回false [英] Mothod always returns false from Firestore DB query

查看:67
本文介绍了Mothod始终从Firestore数据库查询返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种检查列表是否包含用户的方法.由于某种原因,即使用户在列表中,它也始终返回false.该功能确实有效,我知道它确实找到了用户,只是不确定为什么它除了false以外不返回其他任何内容.

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.

方法:

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
}

通话:

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

推荐答案

Firebase API是asynchronous,这意味着onSucces()函数在调用后立即返回,并且将调用它返回的Task中的回调.一段时间以后.无法保证需要多长时间.因此,可能需要几百毫秒到几秒钟的时间才能获得该数据.由于该方法会立即返回,因此您要返回的bool变量的值尚未从回调中填充.

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.

基本上,您正在尝试从异步的API同步返回值.那不是一个好主意.您应该按预期异步处理API.

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.

此问题的快速解决方案是在回调中使用bool变量 only 的值.这也意味着您需要更改checkUserChatChannel而不是返回布尔值以返回void,否则,我建议您从此

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.

这篇关于Mothod始终从Firestore数据库查询返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆