问题描述
我想知道在Firebase(基于Java/Android)上处理多个并行读取操作"的最佳方法是注册一个SingleValueEventListener并处理onDataChanged事件中的数据.假设我有三个不同的DatabaseReference位置,并且立即注册了三个侦听器,
I wonder what is the best way to deal with multiple parallel 'read operations', that is registering a SingleValueEventListener and handling data in onDataChanged event, at Firebase (Java-based/Android). Let´s say, I have three different DatabaseReference locations and three listeners are registered right away,
- 是否有可能将整个东西捆绑在一起,以便只有一个请求?
- 实现等待"的最佳方法是什么,以便在三个DataChange事件中的最后一个传递数据时立即执行代码,而不嵌套它们,以便在以下情况下仅注册以下事件:上一个?
推荐答案
-
没有办法(也不需要)将读取的内容合并为一个请求.Firebase数据库客户端通过单个连接流水线处理请求.参见,它是如何工作的以及为什么它解决了大多数开发人员对往返性能的担忧.
There is no way (nor a need) to combine the reads into a single request. The Firebase database client pipelines the requests over a single connection. See my answer here for how this works and why this addresses the concerns about round trip performance that most developers have.
在由 Promise.all()
处理的JavaScript代码中.在Android中,您可以使用 Task
s完成相同的任务.道格·史蒂文森(Doug Stevenson)为此撰写了一系列博客文章,建议阅读以获取详细信息:第1部分,第2部分,第3部分(涵盖使用 Continuation
进行的链接任务),第4部分(最终 涵盖了使用 Tasks.whenAll(...)
).
In JavaScript code that is handled by Promise.all()
. In Android you can use Task
s to accomplish the same. Doug Stevenson wrote a great series of blog post on this, which I recommend reading for details: part 1, part 2, part 3 (which covers chaining tasks using a Continuation
), part 4 (which finally covers running tasks in parallel using Tasks.whenAll(...)
).
这篇关于在Firebase中处理并行读取操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!