Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.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
Java 循环运行速度比Firestore查询任务快_Java_Android_Firebase_Google Cloud Firestore - Fatal编程技术网

Java 循环运行速度比Firestore查询任务快

Java 循环运行速度比Firestore查询任务快,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,在一个名为“模块下的课程”的集合中,我们在一个集合模块中有2个文档,每个模块分别有5个和6个文档。当我们运行一个循环来解析集合模块中的文档以使用Firestore从集合课程中获取文档时,外部for循环的运行速度比Firestore查询本身快,从而导致值在集合之间交换。我们尝试使用Tasks.whenAllSuccess或Tasks.whenAllComplete找到多个解决方案,但除了让主线程休眠1秒外,其他方法都无效。在本例中,查询获取了适当的值。然而,将线程置于睡眠状态肯定会冻结应用程序,这

在一个名为“模块下的课程”的集合中,我们在一个集合模块中有2个文档,每个模块分别有5个和6个文档。当我们运行一个循环来解析集合模块中的文档以使用Firestore从集合课程中获取文档时,外部for循环的运行速度比Firestore查询本身快,从而导致值在集合之间交换。我们尝试使用Tasks.whenAllSuccess或Tasks.whenAllComplete找到多个解决方案,但除了让主线程休眠1秒外,其他方法都无效。在本例中,查询获取了适当的值。然而,将线程置于睡眠状态肯定会冻结应用程序,这是不可取的。以下是代码片段:

 for (String moduleId : modulesDocumentIds)
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            firebaseFirestore.collection("Users")
                    .document(userDocumentId)
                    .collection("Courses")
                    .document(courseDocumentId)
                    .collection("Modules")
                    .document(moduleId)
                    .collection("Lessons")
                    .orderBy("lesson_number",Query.Direction.ASCENDING)
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {

                            for (QueryDocumentSnapshot queryDocumentSnapshot : task.getResult())
                            {

                                // Showing Lessons


                                             updateCourseCompletion();
                                                }
                                            });

                                        }
                                    }
                                }
                            }


                        }
                    });
在这里,如果ModuleDocumentId有2个字符串值,而不使线程处于睡眠状态,因为for循环的运行速度比Firestore查询任务快,那么将为第二个moduleId字符串获取课程,然后为第一个moduleId字符串获取课程,从而导致值交换。
我们还没有找到解决办法。是否有人可以就此提供建议?

您可以为firestore查询创建一个函数,并在收到for循环项的响应后调用该函数

private fun firebasecall() {
        var module = modulesDocumentIds.get(index)
//place your firestore call here
        firebaseFirestore.collection("Users")
            .document(userDocumentId)
            .collection("Courses")
            .document(courseDocumentId)
            .collection("Modules")
            .document(moduleId)
            .collection("Lessons")
            .orderBy("lesson_number",Query.Direction.ASCENDING)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull (Task<QuerySnapshot>) task) {
                    index++
                    if (index < modulesDocumentIds.size) {
                        firebasecall()
                    } else {
                        //loop completes
                    }

                });

            }
    }

希望有帮助。

您可以为firestore查询创建一个函数,并在收到for循环项的响应后调用该函数

private fun firebasecall() {
        var module = modulesDocumentIds.get(index)
//place your firestore call here
        firebaseFirestore.collection("Users")
            .document(userDocumentId)
            .collection("Courses")
            .document(courseDocumentId)
            .collection("Modules")
            .document(moduleId)
            .collection("Lessons")
            .orderBy("lesson_number",Query.Direction.ASCENDING)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull (Task<QuerySnapshot>) task) {
                    index++
                    if (index < modulesDocumentIds.size) {
                        firebasecall()
                    } else {
                        //loop completes
                    }

                });

            }
    }
希望能有所帮助。

退房。退房。