Is it possible to send a synchronous request in the Firebase?

前端 未结 4 1441
无人共我
无人共我 2020-12-17 10:51

I\'m using Firebase Android SDK and became interested in sending synchronous request instead of asynchronous. According to the documentation, in any request cal

4条回答
  •  情深已故
    2020-12-17 11:18

    While it is not possible to load data from the FirebaseDatabase in a synchronous way, it is possible to wait for the load to finish synchronously.

    You can wrap your value listener in a CountDownLatch and count down, once the onDataChange or onCancelled implementation is called.

    This is actually what the Tasks api is doing internally if you call Tasks.await(someTask).

    You should use the value listener for single event listening, because in this case I assume you don't want continued updates. And use a proper timeout for the CountDownLatch, since Firebase won't timeout, ever.

    reference.addListenerForSingleValueEvent(...);
    

    You also have to take into account, that if you have the FirebaseDatabase cache enabled, the first result might not be the actual value on the server.

    I have to add: While this might work, it is against the idea how firebase is designed and supposed to be used, as Frank already said.

提交回复
热议问题