【问题标题】:Android Cloud Firestore: Query to find a users nameAndroid Cloud Firestore:查询以查找用户名
【发布时间】:2018-11-11 02:54:06
【问题描述】:

我在这里寻找了多种解决方案,但找不到任何特定于我的情况的东西,因此我在这里发布一个问题,同时我仍在继续寻找解决方案。我对 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 变量是布尔值而不是查询对象?

【问题讨论】:

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


【解决方案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 对您没有帮助,因为它会附加一个侦听器以实时获取数据,但这不是您所需要的。您只需获取一次数据。

【讨论】:

  • 啊,我明白了,所以无论查询结果如何,都会创建查询对象。非常感谢。我无法理解查询对象的属性。
  • 是的,你是对的。不管查询的结果是什么,都会创建查询对象。不客气,干杯!
【解决方案2】:

您可以使用布尔变量作为 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。现在,使用 if else 可以根据 nameFound 的值调用你的认证方法。

【讨论】:

  • 如果这对你有用@Adrian Robertson,请勾选我的答案下方左侧的勾号图标,将答案标记为已接受。
猜你喜欢
  • 1970-01-01
  • 2021-05-11
  • 2017-05-15
  • 1970-01-01
  • 2018-10-15
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
相关资源
最近更新 更多