关于Firestore查询数据文档,特别是DocumentSnapshot [英] About the Firestore query-data documentation specifically DocumentSnapshot

查看:58
本文介绍了关于Firestore查询数据文档,特别是DocumentSnapshot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firestore查询数据获取数据中我想知道 document!= null 在什么情况下会评估为false?相反,它应该不是!document.exists()

In the Firestore query-data get-data docs I want to know in what situation the document != null will evaluate to false? Shouldn't it instead be !document.exists()

DocumentReference docRef = db.collection("cities").document("SF");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document != null) {
                Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

推荐答案

onComplete()回调提供了Google

The onComplete() callback provides a Google Task instance of Task<DocumentSnapshot> and calling getResult() on this should return a DocumentSnapshot, and never null.

这引起了我的兴趣,所以我做了一些测试:我在我不知道的文档上附加了 OnCompleteListener :

This piqued my interest though so I did a little testing: I attached an OnCompleteListener to a document that I know doesn't exist:

FirebaseFirestore.getInstance()
        .collection("this-does-not-exist")
        .document("neither-does-this")
        .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    if (task.getResult() == null) Log.d(TAG, "getResult is null");
                    Log.d(TAG, "getResult: " + task.getResult());
                }
            }
});

执行后, task.getResult()== null 检查结果为 false ,因此消息"getResult为null"不是不是写入日志.

When executed, the task.getResult() == null check evaluates to false and therefore the message "getResult is null" is not written to the log.

但是,在 getResult()的返回值上调用 toString()会引发以下错误:

However, calling toString() on the return from getResult() throws the below error:

java.lang.IllegalStateException: This document doesn't exist. Use DocumentSnapshot.exists() to check whether the document exists before accessing its fields.

这明确指出要使用说:

This explicitly states to use exists() rather than a null-check, but the documentation for "get a document" says:

注意:如果 docRef 所引用的位置没有文档,则生成的 document 将为空.

Note: If there is no document at the location referenced by docRef, the resulting document will be null.

此外,同一文档页面上其他语言的示例都使用 exists(),但Android和Objective-C除外.最重要的是:Java示例使用 exists():

Furthermore, examples in other languages on the same documentation page all use exists(), except Android and Objective-C. Most importantly: the Java example uses exists():

DocumentReference docRef = db.collection("cities").document("SF");
// asynchronously retrieve the document
ApiFuture<DocumentSnapshot> future = docRef.get();
// ...
// future.get() blocks on response
DocumentSnapshot document = future.get();
if (document.exists()) {
  System.out.println("Document data: " + document.getData());
} else {
  System.out.println("No such document!");
}

在这种情况下,我敢打赌这似乎是文档中的错误,我们应该使用 document.exists()而不是 document!= null .

In this case, I'd wager that this seems to be an error in the documentation, and we should be using document.exists() rather than document != null.

这篇关于关于Firestore查询数据文档,特别是DocumentSnapshot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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