Do we need to use background thread for retrieving data using firebase?

前端 未结 3 444
生来不讨喜
生来不讨喜 2020-12-06 05:29

I\'ve an android app where I\'m retrieving data into a Fragment. And I believe that Firebase manages its asynchronous calls. But still I\'ve doubt the if we need to write th

相关标签:
3条回答
  • 2020-12-06 05:43

    Firebase runs all of its callbacks asynchronously as documented https://www.firebase.com/docs/android/guide/retrieving-data.html . This is done through a web socket layer.

    If for example, you need to do a large amount of data processing on the result of the Firebase data update - you should probably spin up an AsyncTask to prevent the UI from blocking. This isn't any different from how you would normally approach data processing before being presented to the UI.

    The Firebase documentation covers how data is handled and the reason why you do not need to execute any background reads. You should probably spend some time reading the documentation.

    0 讨论(0)
  • 2020-12-06 05:49

    The Firebase Database client performs all network and disk operations off the main thread.

    The Firebase Database client invokes all callbacks to your code on the main thread.

    So network and disk access for the database are no reason to spin up your own threads or use background tasks. But if you do disk, network I/O or CPU intensive operations in the callback, you might need to perform those off the main thread yourself.

    0 讨论(0)
  • 2020-12-06 06:00

    If you're pulling down a large-ish collection of data from the database, and you want to convert that all into a JavaBean type collection, you may want to offload that onto another thread as the size of data its use of reflection may cause too much work for the main thread. The only way to know about this for sure is to benchmark it yourself. Generally speaking, you get 16ms to do things on the main thread before you start dropping from the optimal rendering speed of 60 frames per second.

    I recently tweeted a diff on a project of mine where I refactored a pattern for sending database listener to an Executor for background processing. However, your app may not call for this kind of complexity. It was good for my app, however. https://twitter.com/CodingDoug/status/773277680867258368

    0 讨论(0)
提交回复
热议问题