Android Cloud Firestore:查询以查找用户名

时间:2018-06-01 02:41:44

标签: java android firebase firebase-realtime-database google-cloud-firestore

我在这里寻找多种解决方案,但无法找到符合我情况的任何内容,因此我在此处发布了一个问题,同时我仍在继续寻找解决方案。我仍然对Firestore相当新,他们的指南/文档仍然不清楚。

我的手机应用程序有一个让用户输入名字的系统。此名称用于遍历Firestore数据库,如果名称作为其中一个用户的字段存在,则该方法必须返回true的布尔值。

此查询将由"继续按钮"触发。这是我的主要活动,如下所示:

    //Authenticate user and proceed to next activity
    continueBtn = (Button) findViewById(R.id.continue_btn);
    continueBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //On click create a db reference and perform a query on it to find current user
            //and authenticate it.
            CollectionReference myRef = database.collection("users");
            Query findNameQ = myRef.whereEqualTo("name", mUserName);
            authenticateUser(findNameQ, mUserName);//I need to pass to this method a variable 'findNameQ' which can be used to validate the existence of a user.
            //mUserName is the name it's looking for.
        }

    });

运行查询后,它会运行authenticateUser方法,该方法基本上验证了用户的存在,并在用户不存在时创建一个新用户。这是方法:

private void authenticateUser(Query findNameQ, String mUserName)
{
    //Read from database and check if user exists
    //if current users name matches to one in database then set userExists to true.
    if (findNameQ != null)
    {
        userExists = true;
        Toast.makeText(this, "User exists!", Toast.LENGTH_SHORT).show();
    }
        Toast.makeText(this, "User doesn't exist!", Toast.LENGTH_SHORT).show();
}

我想使用if(findNameQ!= false)而不是null,我该怎么做才能使我的findNameQ变量是布尔值而不是查询对象?

2 个答案:

答案 0 :(得分:1)

您可以使用布尔变量作为 boolean nameFound = false;

现在,将快照侦听器附加到查询以检查名称是否存在:

    findNameQ.addSnapshotListener(new EventListener<QuerySnapshot>(){
                @Override
                public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
                    for (DocumentSnapshot ds: queryDocumentSnapshots){
                        if (ds!=null && ds.exists()){
                            Toast.makeText(RegisterActivity.this, "Username Exists!", Toast.LENGTH_SHORT).show();
                            nameFound = true;
                        }
                    }
                }
            });

否则将使用nameFound的默认值false。现在,use可以使用if else根据nameFound的值调用您的身份验证方法。

答案 1 :(得分:1)

为了了解Firestore数据库中是否存在用户名,您需要使用get()调用。仅创建一个Query对象不会为您提供太多帮助。除此之外,如果您正在检查findNameQ != null,它将始终评估为true,因为findNameQ对象已创建且永远不会为null。因此,要解决此问题,请使用以下代码行:

productsRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                if (document.exists()) {
                    authenticateUser(findNameQ, mUserName);
                }
            }
        }
    }
});

请注意,使用addSnapshotListener对您没有帮助,因为它会附加一个监听器来实时获取数据,但这不是您所需要的。您只需要获取一次数据。