Firebase数据库获取节点值 [英] Firebase Database getting Node Value

查看:202
本文介绍了Firebase数据库获取节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Firebase实时数据库中获取我的wgID,它的位置为/WG/FlatNumber/ 现在这是我的代码:

I want to get my wgID from my Firebase RealTime Database, it is located as a value of /WG/FlatNumber/ Now here is my Code:

MainActivity:

MainActivity:

System.out.println("1");
dbContact.schreibeFlatObjekt();
System.out.println("7");

schreibeFlatObjekt:

schreibeFlatObjekt:

//wgID is global
private String wgID;
public schreibeFlatObjekt(){
//CountDownLatch to wait for the Listener
final CountDownLatch cdl = new CountDownLatch(1);


    ValueEventListener listener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            System.out.println("3");
            //Getting the Data from the snapshot
            wgID = (String) dataSnapshot.getValue();
            System.out.println("4 wgID: " + wgID);
            //counting down the cdl so the cdl.await() can go on 
            cdl.countDown();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

    System.out.println("2");
    fref.child("WG").child("FlatNumber").addListenerForSingleValueEvent(listener);
    System.out.println("5");

    try{
        cdl.await();
    }
    catch(Exception e)
    {
        System.out.println(e.toString());
    }
    System.out.println("6 wgID: " +wgID);
    return 0;

所以当我不使用CountdownLatch时,System.out.println给我这个:

So when I don't use the CountdownLatch, the System.out.println give me this:

I/System.out: 1
I/System.out: 2
I/System.out: 5
I/System.out: 6 wgID: null
I/System.out: 7
I/System.out: 3
I/System.out: 4 wgID: 1

据我所见,在方法已经准备好之后,将执行监听器.

So as I see it, the Listener is executed, after the method is already ready.

如果我使用CountDownLatch,它只会运行直到出现"cdl.await()",然后才停止,该监听器将永远不会执行,并且该应用程序也会停止.

If I use the CountDownLatch, it only runs until it comes to "cdl.await()" and then just stops, the Listener is never executed and the app stops.

所以我如何获取我的ID,我的意思是我只想从数据库中获取一个值,您可以使用"setValue()"进行设置,但不能使用"getValue"或其他方式读取它,必须使用听众?

So how do I get my ID, I mean I just want one Value from my Database, you can set it with "setValue()" but you cannot read it with "getValue" or something, it has to be done with listeners?

推荐答案

问题是由于两个事实造成的:

The problem is due to two facts:

  1. cdl.await()在主线程上运行,阻止它直到通知.
  2. Firebase回调也有望在主线程上发生.
  1. cdl.await() runs on the main thread, blocks it until notified.
  2. The Firebase callback also expects to happen on the main thread.

由于cdl.await()在数据库回调之前发生,它会无限期地阻塞主线程,从而防止了回调的发生(因为它已经排队等待在无限期阻塞的主线程上运行).

Since cdl.await() happens before the database callback, it blocks the the main thread indefinitely, which prevents the callback from happening (since it's queued up to run on the main thread, which is blocked indefinitely).

解决方案是根本不使用CountDownLatch. Firebase API都是异步的,调用者应期望在主线程上执行查询后结果会到达.执行应该从该回调继续进行,而不是从等待闩锁的地方继续执行.

The solution is not to use a CountDownLatch at all here at all. Firebase APIs are all asynchronous, and the caller should expect that the result arrives after the query is performed on the main thread. Execution should continue from that callback, not from something waiting on a latch.

这篇关于Firebase数据库获取节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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