【发布时间】:2018-04-04 09:36:19
【问题描述】:
我在我的 Android 应用程序的单独线程中使用 Cloud Firestore,因此我不想使用侦听器 OnSuccessListener 和 OnFailureListener 在另一个线程中运行。我可以让我的线程等待结果(并在需要时捕获任何异常)吗?
目前查询的代码是这样的:
FirebaseFirestore.getInstance().collection("someCollection").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot documentSnapshots) {
// do something on the UI thread with the retrieved data
}
});
我想要什么:
FirebaseFirestore.getInstance().collection("someCollection").getAndWaitForResult();
//Block the thread and wait for result, no callbacks.
//getAndWaitForResult() is not a real function, just something to describe my intention.
我以前用 Parse Server 工作过,那里很简单。
【问题讨论】:
-
如果我理解正确,您可能不得不取消您在
onStop或onPause开始的请求。 -
并非如此。我添加了一些代码来说明。
-
您可以使用
EventListener并使用一些自定义类来实现它,然后使用onEvent()触发您想要的内容 -
EventListener 会触发异步操作,我特意问有没有办法避免这种情况。
标签: java android firebase google-cloud-firestore