【问题标题】:How do I wait until my data is retrieved from Firebase? [duplicate]如何等到从 Firebase 检索到我的数据? [复制]
【发布时间】:2019-09-11 02:38:14
【问题描述】:

我的问题是,在结束我的主要方法之前,我需要等待通过方法“onDataChange”检索来自 Firebase 的数据。

我的代码是(CalendarDbHelper):

public synchronized ArrayList<Day> listDays()
    {
       final  CountDownLatch done = new CountDownLatch(1);
        db.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists())
                {
                    for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
                    {
                        Day day=postSnapShot.getValue(Day.class);
                        listDays.add(day);

                    }
                }
               done.countDown();
            }


            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }

        });

        try {
            done.await(); 
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
        return listDays;

    }

如您所见,我已经在尝试使用 CountDownLatch 等待,但它不起作用:卡住了。

我需要活动“CalendarActivity”的listDays,我像这样使用它:

 //Real initialization of the database
        db = FirebaseDatabase.getInstance().getReference();
        mDatabase = new CalendarDbHelper(db);
        final List<Day> allDays = mDatabase.listDays();



        //Activating things to click on
        if(allDays.size() > 0){
            calendarRV.setVisibility(View.VISIBLE);
            calendarRVAdapter = new CalendarAdapter(this, allDays, new CalendarAdapter.OnListItemClickListener() {

                @Override
                public void onListItemClick(int clickedItemIndex) {
                    String id = allDays.get(clickedItemIndex).getId();
                    MyCustomAlertDialog(id);
                }
            });
            calendarRV.setAdapter(calendarRVAdapter);
        }else {
            calendarRV.setVisibility(View.GONE);
            Toast.makeText(this, "There is no product in the database. Start adding now", Toast.LENGTH_LONG).show();
        }

【问题讨论】:

  • 你试过Handler了吗?后延迟方法??
  • 你的 Handler 是什么意思?而且我也从未听说过后延迟方法:c
  • 您希望在一段时间后调用方法 db.addValueEventListener() 对吗??
  • 不是真的,确实我想等待 onDataChange() 方法完成检索数据,然后返回 listDays,因为如果不等待,返回的 listDays 为 null
  • 现在我遇到了你的问题。听着,数据是从 Firebase 异步加载的。你需要了解那件事。

标签: android firebase firebase-realtime-database


【解决方案1】:

如果这是你的“主要方法”,你应该在onDataChange被触发时放置返回数据

【讨论】:

  • 我真的不明白我应该怎么做。你想让我在 onDataChange 里面放一个“return listDays”吗?
  • @Nouxy 是你的“主要方法”调用这个函数来获取数据???
  • 如果是,则从main调用此方法但不返回数据。保持视图显示进度。在onDatachange中,在返回数据时执行更新UI的功能。如果你只想做 1 次,清除 datachange 监听器
  • 对不起,“主要方法”是什么意思?我真的不明白你要我做什么
  • 私有 OnGetDataDone onDone; CalendarDbHelper(db,OnGetDataDone onDone){ this.onDone = onDone; } public interface OnGetDataDone{ void onDone(ListDay listday) } 将此添加到您的 CalendarDbHelper 并将所有更新 ui 的代码放在您的 CalendarActivity 中的 onDone 方法覆盖中
【解决方案2】:

试试这个。

 public synchronized ArrayList<Day> listDays()
    {
   final  CountDownLatch done = new CountDownLatch(1);
    db.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) 
{
            if(dataSnapshot.exists())
            {
                for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
                {
                    Day day=postSnapShot.getValue(Day.class);
                    listDays.add(day);

                }

             // Do whatever you want to do with your list from here..

            }
           done.countDown();
        }


        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }

    });

    try {
        done.await(); 
    } catch(InterruptedException e) {
        e.printStackTrace();
    }
    return listDays;

}

【讨论】:

  • 它不起作用,因为在 "listDays.add(day);" 行上无法识别在 "for" 循环中创建的 Day这是在“for”循环之外:/
  • 所以最终的解决方案是更新的答案。几天前,我也陷入了这种情况。我试过这种方式来工作。一切都很完美。
  • 我可以看看你的完整代码吗?您在代码中的哪个位置使用该 listDays??
  • 我刚刚上传了我在代码中使用 listDays 的编辑 :)
猜你喜欢
  • 2015-08-19
  • 2018-12-14
  • 2020-09-02
  • 1970-01-01
  • 2018-08-29
  • 2016-12-18
  • 2018-11-07
  • 2021-10-01
相关资源
最近更新 更多