Firebase数据库中的异步侦听器

时间:2018-07-13 03:11:03

标签: android firebase asynchronous google-cloud-firestore

      Task task1=  firestore.collection(collection).document(city).collection("Bigads").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {

            if (task1.isSuccessful()) {
                if (task1.getResult().isEmpty()) {
                } else {
                    for (DocumentSnapshot docs : task.getResult()) {

                        AdsModel ad = new AdsModel(docs.getString("For"), docs.getString("ImageURL"), docs.getString(" Product"), docs.getString("ShopUID"));

                        Bdata.add(ad);
                        homeMainAdapter.notifyDataSetChanged();
                        recent_show_all.setVisibility(View.VISIBLE);
                        rvd.setVisibility(View.VISIBLE);
                        recentRView.setVisibility(View.VISIBLE);
                        end.setVisibility(View.VISIBLE);
                    }
                }

            } else {


            }

        }
    });
  

在task2中,它们是一个if条件,我需要检查task1是否为空。如果task2在task1之前运行,则两个侦听器都是异步的,那么if条件将变为true并且结果将是错误的,如何处理此任务该task1应该先运行,然后再运行task2?

Task task2= firestore.collection("user").document(uid).collection("rvd").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {


                if (task.isSuccessful() && task1.getResult().isEmpty()) {      



                    for (DocumentSnapshot doc : task.getResult()) {

                        RvdModel r = new RvdModel(doc.get("image1").toString(), doc.get("ProductUID").toString(), doc.get("Brand").toString());
                        Rdata.add(r);
                        rvdAdapter.notifyDataSetChanged();

                        Toast.makeText(MainActivity.this, "working", Toast.LENGTH_SHORT).show();
                        suggestionRView.setVisibility(View.VISIBLE);
                        suggest.setVisibility(View.VISIBLE);
                        suggest_show_all.setVisibility(View.VISIBLE);
                    }

                } else {
                    Toast.makeText(MainActivity.this, "errororr", Toast.LENGTH_SHORT).show();
                }
            }
        });

1 个答案:

答案 0 :(得分:0)

您可以将Task2放在onComplete的{​​{1}}内

Task1