在Firebase中的多个地点接收数据后继续执行 [英] Continue execution after data received from multiple location in Firebase

查看:117
本文介绍了在Firebase中的多个地点接收数据后继续执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是一个常见的问题,但我真的坚持在这一点上。

我从2个地方接收数据,从两个地方收到数据后,我需要继续执行,并将数据返回给调用方法。

我知道这个线程: https://stackoverflow.com/ a / 33204705/1820644 ,但实际上这并不适合,因为我需要将数据返回给调用方法。

对于阻塞UI线程的方法,我可以从 AsyncTask 调用它,这是没有问题的。但是,我怎样才能返回数据到调用方法,我已经成功地完成执行。

这是我的助手类里面

  //这个方法应该在AsyncTask中调用
public boolean doComputation(){
DatabaseReference ref = FirebaseDatabase.getInstance()。getReference()。child activity)
.child(id);
$ b $ ref.child(path1)。addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
// Call 1已完成
}

@Override
public void onCancelled(DatabaseError databaseError){


});

ref.child(path2)。addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
// Call 2已完成
}

@Override
public void onCancelled(DatabaseError databaseError){


});

//一些要做的事

//执行完成后需要返回true
return true;



$ b

我不能在里面返回true onDataChange ,因为它会计入 onDataChange 方法,而不是用于 doComputation



我可以通过将其移动到另一个方法来执行计算,并且在每个onDataChange回调之后,我可以检查变量计数,如果是 2 ,则可以执行计算。但完成后,我需要通知调用方法,执行已完成。

这是有点棘手, Firebase 。但是,我现在真的被困住了。任何帮助将不胜感激。谢谢。

解决方案

我已经用了 Tasks API 已经使用了。它是伟大的。

正如@qbix所述,这个答案做同样的事情。答案中的例子很好解释。

你也可以找到这个API说明的视频链接这里



我已经试过并测试过了。解决我的问题。


I know this might be a common question but I am really stuck at this point.

I am receiving data from 2 multiple locations and after I received from both I need to continue executing and than return that data to the calling method.

I am aware of this thread: https://stackoverflow.com/a/33204705/1820644 but it doesn't fit here actually as I need to return the data to the calling method.

For the method that blocks UI thread I can call it from AsyncTask, there is no problem. But how can I return data to the calling method that I have successfully completed execution.

This is inside my helper class

// This method should be called in AsyncTask
public boolean doComputation() {
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("activity")
                .child(id);

        ref.child("path1").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
               // Call 1 completed
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        ref.child("path2").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
               // Call 2 completed
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        // Some more to do

        // Need to return true after execution complete
        return true;
}

I can not return true inside of the onDataChange as it will counted for onDataChange method and not for doComputation.

I can perform the calculation by moving it to another method and after each onDataChange callback I can check for variable count, and if it is 2 I can perform the calculations. But after it completes I need to notify it to the calling method that execution is completed.

This is a little bit tricky with Firebase. But, I am really stuck at it right now. Any help will be much appreciated. Thank you.

解决方案

I have gone with the Tasks API which Firebase uses already. Its great.

As mentioned by @qbix , This answer does the same thing. The example in the answer explains good.

You can also find video link of this API instructions here.

I have tried and tested it. Solves my problem.

这篇关于在Firebase中的多个地点接收数据后继续执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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