我们是否需要使用后台线程来使用Firebase检索数据? [英] Do we need to use background thread for retrieving data using firebase?

查看:111
本文介绍了我们是否需要使用后台线程来使用Firebase检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,我正在将数据检索到一个片段。我相信Firebase管理着它的异步调用。但是我仍然怀疑是否需要在后台线程中写入Firebase代码?



如果我们需要将它写入后台线程,那么可以你请告诉哪些操作需要更多的时间。例如:

  mDatabase = FirebaseDatabase.getInstance()。getReference()。child(Blog); 

我认为在主UI线程上执行此操作可能会导致完全风险,因为设置数据库之间的连接可能有时Firebase数据库客户端执行所有网络和磁盘操作 off 主线程。


$ b Firebase数据库客户端会在主线程上调用您的代码的所有回调函数。



<因此,数据库的网络和磁盘访问没有理由使自己的线程或使用后台任务。但是如果在回调中执行磁盘,网络I / O或CPU密集型操作,则可能需要自行执行主线程。


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 the Firebase code in the background thread or not?.

If we need to write it into the background thread then can you please tell which operations takes more time. eg:

mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");

I think that performing this on the main UI thread may become risk full because setting connection between database may sometime take large time.

解决方案

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.

这篇关于我们是否需要使用后台线程来使用Firebase检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆