是否有可能从Firebase同步加载数据? [英] Is it possible to synchronously load data from Firebase?

查看:137
本文介绍了是否有可能从Firebase同步加载数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用通过Firebase连接的对等数据更新Android应用程序中WebView的部分内容。为此,执行将返回所需数据的阻塞操作会很有帮助。例如,聊天示例的实现将等待另一个聊天参与者在push.setValue()返回之前写入某个内容。
Firebase有这种行为吗?

解决方案

在常规的JVM上,同步原语。



例如:

  //创建一个java.util。并发。信号量0初始许可证
最终信号量信号量=新的信号量(0);

//将值监听器附加到Firebase引用
ref.addValueEventListener(new ValueEventListener(){
// onDataChange将在当前值加载时执行, b $ b @Override
public void onDataChange(DataSnapshot dataSnapshot){
// TODO:做任何你需要做的事情用dataSnapshot

//告诉调用者我们'重做
semaphore.release();
}
$ b $ @覆盖
public void onCancelled(FirebaseError firebaseError){

}
});

//等待onDataChange回调释放了信号量
semaphore.acquire();

//发送我们的回复信息
ref.push()。setValue(哦,真的?这是我想到的);

但这不适用于Android。这是一件好事,因为在影响用户界面的任何事情中使用这种类型的阻塞方法是一个坏主意。我使用这个代码的唯一原因是因为我需要进行单元测试。

在真正的面向用户的代码中,您应该使用事件驱动的方法。所以,不要等待数据来,然后发送我的消息,而是当数据进来,发送我的消息:

  //将值侦听器附加到Firebase引用
ref.addValueEventListener(new ValueEventListener(){
// onDataChange将在当前值加载并更改时执行
@Override
public void onDataChange(DataSnapshot dataSnapshot){
// TODO:做任何你需要做的事情用dataSnapshot

//发送我们的响应信息
);
}
$ b @Override
public void onCancelled(FirebaseError firebaseError){

}
});

最终结果是完全一样的,但是这段代码不需要同步,也不会阻塞在Android上。


I'm trying to update parts of a WebView in my Android app with data I'm getting from a peer connected via Firebase. For that, it could be helpful to execute blocking operations that will return the needed data. For example, an implementation of the Chat example that will wait until another chat participant writes something before the push.setValue() to return. Is such a behavior possible with Firebase?

解决方案

On a regular JVM, you'd do this with regular Java synchronization primitives.

For example:

// create a java.util.concurrent.Semaphore with 0 initial permits
final Semaphore semaphore = new Semaphore(0);

// attach a value listener to a Firebase reference
ref.addValueEventListener(new ValueEventListener() {
    // onDataChange will execute when the current value loaded and whenever it changes
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // TODO: do whatever you need to do with the dataSnapshot

        // tell the caller that we're done
        semaphore.release();
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

// wait until the onDataChange callback has released the semaphore
semaphore.acquire();

// send our response message
ref.push().setValue("Oh really? Here is what I think of that");

But this won't work on Android. And that's a Good Thing, because it is a bad idea to use this type of blocking approach in anything that affects the user interface. The only reason I had this code lying around is because I needed in a unit test.

In real user-facing code, you should go for an event driven approach. So instead of "wait for the data to come and and then send my message", I would "when the data comes in, send my message":

// attach a value listener to a Firebase reference
ref.addValueEventListener(new ValueEventListener() {
    // onDataChange will execute when the current value loaded and whenever it changes
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // TODO: do whatever you need to do with the dataSnapshot

        // send our response message
        ref.push().setValue("Oh really? Here is what I think of that!");
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

The net result is exactly the same, but this code doesn't required synchronization and doesn't block on Android.

这篇关于是否有可能从Firebase同步加载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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