Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 检查集合是否存在-Firestore_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

Android 检查集合是否存在-Firestore

Android 检查集合是否存在-Firestore,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,例如: 在继续执行事件调度功能之前,我必须与Firestore预约 Example in the database: - Companie1 (Document) --- name --- phone --- Schedules (Collection) -----Event 1 -----Event 2 我有一个执行新日程安排的函数 根据例子。 我需要检查Schedules集合是否存在 如果不存在,则执行调度功能。如果我已经存在,我需要做另一个程序 我已经使用了这种模式,但并不正确 db.c

例如:

在继续执行事件调度功能之前,我必须与Firestore预约

Example in the database:
- Companie1 (Document)
--- name
--- phone
--- Schedules (Collection)
-----Event 1
-----Event 2
我有一个执行新日程安排的函数

根据例子。 我需要检查Schedules集合是否存在

如果不存在,则执行调度功能。如果我已经存在,我需要做另一个程序

我已经使用了这种模式,但并不正确

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());
                }
            }
        });
db.collection(“公司”)文档(idcompanye1)
.get()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
对于(DocumentSnapshot文档:task.getResult()){
}
}否则{
Log.d(标记“获取文档时出错:”,task.getException());
}
}
});

在继续注册之前,我需要帮助找到执行此操作的方法。

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

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");
}
任务的结果是
文档快照
。通过该方法可以查看基础文档是否实际存在

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

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());
            }
        }
    });

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

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");
}
任务的结果是
文档快照
。通过该方法可以查看基础文档是否实际存在

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

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());
            }
        }
    });

我需要检查收集计划是否存在。所以我只是检查它是否有任何数据@AlexMamoYes,看看ti是否为空。如果不是,则表示包含信息。是的,但您将始终拥有这些信息。除了收款计划。这一个可能是也可能不是。我只需要检查它是否存在@AlexMamoEven使用getData无法检查集合是否存在@AlexMamoYou不需要使用getData()。请查看我的更新答案。我需要检查收集计划是否存在。所以我只需检查它是否有任何数据@AlexMamoYes,看看ti是否为空。如果不是,则表示包含信息。是的,但您将始终拥有这些信息。除了收款计划。这一个可能是也可能不是。我只需要检查它是否存在@AlexMamoEven使用getData无法检查集合是否存在@AlexMamoYou不需要使用getData()。请看我的最新答案。