0

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,

  1. Is it possible to bundle the whole thing somehow so there would be just one request?
  2. What is the best way to implement a 'wait for', so that code is executed as soon as the last of the three DataChange Events delivers data, without nesting them, so that following would just be registered in the event of the previous one?
Lemao1981
  • 2,225
  • 5
  • 18
  • 30

1 Answers1

3
  1. 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.

  2. In JavaScript code that is handled by Promise.all(). In Android you can use Tasks 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(...)).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807