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

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

问题描述

我正在尝试使用从通过 Firebase 连接的对等方获取的数据更新我的 Android 应用程序中的部分 WebView.为此,执行将返回所需数据的阻塞操作可能会有所帮助.例如,Chat 示例的一个实现将等到另一个聊天参与者在 push.setValue() 之前写入一些东西才能返回.Firebase 是否可以实现这种行为?

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?

推荐答案

在常规 JVM 上,您可以使用常规 Java 同步原语来执行此操作.

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

例如:

// 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");

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

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) {
        throw firebaseError.toException();
    }
});

最终结果完全相同,但此代码不需要同步,也不会在 Android 上阻塞.

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

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

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