如何等待直到从Firebase检索数据?

时间:2019-04-21 17:58:24

标签: android firebase firebase-realtime-database

我的问题是,在结束我的主要方法之前,我需要等待Firebase中的数据在“ onDataChange”方法上进行检索。

我的代码是(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();
        }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

尝试一下。

 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;

}