检查集合的存在-Firestore [英] check collection existence - Firestore

查看:53
本文介绍了检查集合的存在-Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

我必须先与Firestore约会,然后再进行事件调度功能.

I have to make an appointment with the Firestore before proceeding with the event scheduling function.

Example in the database:
- Companie1 (Document)
--- name
--- phone
--- Schedules (Collection)
-----Event 1
-----Event 2

我有一个执行新时间表的功能.

I have a function that performs a new schedule.

根据示例.我需要检查Schedules集合是否存在.

According to the example. I need to check if the Schedules collection exists.

如果不存在,则执行调度功能.如果已经存在,则需要执行其他步骤.

If it does not exist I execute the scheduling function. If I already exist I need to do another procedure.

我已经使用了这种模式,而不是正确的.

I already used this pattern and not right.

db.collection("Companies").document(IDCOMPANIE1)
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {

                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

在进行注册之前,我需要帮助找到解决方法.

I need help finding a way to do this before proceeding with the registration.

推荐答案

要实现此目的,只需检查是否为空:

The achieve this just check for nullity:

DocumentSnapshot document = task.getResult();
if (document != null) {
    Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
    //Do the registration
} else {
    Log.d(TAG, "No such document");
}

Task 的结果是 DocumentSnapshot .基础文档是否实际存在可以通过

The result of this Task is a DocumentSnapshot. Whether or not the underlying document actually exists is available via the exists() method.

如果该文档确实存在,则可以调用getData获取其内容.

If the document does exist you can call getData to get at the contents of it.

db.collection("Companies").document(IDCOMPANIE1)
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (DocumentSnapshot document : task.getResult()) {
                    if(document.exists()) {
                        //Do something
                    } else {
                        //Do something else
                    }

                }
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });

如果您想知道 task 是否为空,请使用以下代码行:

If you want know if the task is empty, please use the following line of code:

boolean isEmpty = task.getResult().isEmpty();

这篇关于检查集合的存在-Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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