【发布时间】: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 变量是布尔值而不是查询对象?
【问题讨论】:
-
您将不得不实际执行该查询。从 Firestore 读取数据的文档可以在这里找到:firebase.google.com/docs/firestore/query-data/get-data - 你一定要熟悉它是如何工作的。
标签: java android firebase firebase-realtime-database google-cloud-firestore