Firebase数据库-在不同的线程上运行 [英] Firebase database - run on different thread
问题描述
我想在其他线程上运行firebase事件.在Firebase的最新版本中,我有执行此代码的代码
I want to run the events of firebase on different thread. On the last version of firebase I had this code that did it
Config firebaseConfig = new Config();
firebaseConfig.setEventTarget(new EventTarget() {
ExecutorService executor = Executors.newSingleThreadExecutor();
@Override
public void postEvent(Runnable runnable) {
executor.execute(runnable);
}
@Override
public void shutdown() {
executor.shutdown();
}
@Override
public void restart() {
}
});
Firebase.setDefaultConfig(firebaseConfig);
如何在新的api中做到这一点?他们是一种方式还是我必须自己实现? (创建每个函数的runnable并在执行程序中运行它)
How can I do it in the new api? Their is a way or I have to implement it by my self? (create runnable of every function and run it in the executor)
推荐答案
Firebase数据库客户端在单独的线程上执行所有网络,磁盘I/O和其他维护.然后,它会在主线程上显示对代码的回调,以便您可以与UI进行交互.
The Firebase Database client performs all networking, disk I/O and other maintenance on a separate thread. It then surfaces the callbacks to your code on the main thread, so that you can interact with the UI.
在大多数情况下,您无需执行任何特殊操作,只需让Firebase客户端处理跨线程处理即可.仅当您需要在回调中做一些繁重的工作(例如onDataChange()
)时,才需要再次在主线程上运行该工作.您可以为此使用通常的Android线程机制.
In most situations you don't have to do anything special and can just let the Firebase client deal with the cross-threading handling. Only when you need to do some heavy work in your callback (e.g. onDataChange()
) will you have to run that work off the main thread again. You can use the usual Android threading mechanisms for that.
这篇关于Firebase数据库-在不同的线程上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!