【发布时间】:2020-05-26 09:22:46
【问题描述】:
您看,我创建了一个 Google 登录方法,然后在该屏幕上首次登录时应该只显示一个设置,他将输入一些关于他自己的信息,这些信息将被上传到一个文档中到 firestore。该 firestore 文档将具有与 google 登录用户相同的 UID。为了弄清楚用户是否已经有一个提供了一些信息的帐户,我想查询一个文档是否已经存在,并以该特定 uid 作为其文档名。
FirebaseUser user = mAuth.getCurrentUser();
FirebaseFirestore db;
String currentID = mAuth.getCurrentUser().getUid();
// Checken ob User schon Dokument hat
db = FirebaseFirestore.getInstance();
db.collection("users")
.whereEqualTo("uid", currentID)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { // Add the listener callback
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
// Check the size of the result to see if any matches were found
if(task.getResult().size() == 0) {
// No document exists containing the searched value
} else {
// A document already exists containing the searched value
hasDocument = true;
}
} else {
Log.e(TAG, "There was an error querying the documents.", task.getException());
}
}
});
if(hasDocument) {
SendUserToMainActivity();
} else {
SendUserToSetupActivity();
Toast.makeText(LoginActivity.this, "You must provide some information first", Toast.LENGTH_LONG).show();
}
我认为我的问题是我正在查询字段,但我不知道如何查询文档名而不是字段本身。我该怎么做?
这是文档的结构方式:
【问题讨论】:
-
您不能简单地在
onComplete()方法之外使用hasDocument的值。 Firebase API 是异步的。因此,请检查重复项,以了解如何使用自定义回调解决此问题。
标签: java android firebase google-cloud-firestore