【发布时间】:2020-01-17 00:44:34
【问题描述】:
我们在“模块”集合中有 2 个文档,它们又分别有 5 个和 6 个文档,位于“模块”下的一个名为“课程”的集合中。当我们运行一个循环来解析集合“Modules”中的文档以使用 Firestore 从集合“Lessons”中获取文档时,外部 for 循环比 firestore 查询本身运行得更快,导致值在集合之间互换。我们尝试使用 Tasks.whenAllSuccess() 或 Tasks.whenAllComplete() 找到多种解决方案,但除了让主线程休眠 1 秒外,没有任何效果。在这种情况下,查询获取了适当的值。但是,让线程进入睡眠状态肯定会冻结应用程序,这是不可取的。下面附上代码sn-ps:
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();
}
});
}
}
}
}
}
});
这里,如果 moduleDocumentIds 有 2 个 String 值,但没有让线程休眠,因为 for 循环比 Firestore Query 任务运行得快,所以为第二个 moduleId 字符串获取课程,然后为第一个获取课程moduleId 字符串,导致值交换。 我们还不能为此找到解决方案。有人可以请教一下吗?
【问题讨论】:
-
签出this。
标签: java android firebase google-cloud-firestore