如何获取/处理具有多个条目的同步Firestore数据?

时间:2019-10-04 18:52:13

标签: android google-cloud-firestore

需要这样做:
    基本上,我希望Firestore =>集合“ order” =>每个文档上都有customerid和productid的文档=> onSuccess =>添加到OrderPOJOList =>调用getCustomerName()然后getProductName()=> 按顺序获取名称 =>添加到相应的ArrayList =>最后,将来自三个arraylist的所有数据组合在一起(OrderPOJOList,CustomerName,ProductName) CurrentOrderPOJOList =>设置为Adapter。

问题:     getCustomerName()和getProductName()中的两个侦听器异步运行,并将Name随机添加到 arrayList ......我想要的只是以顺序在适配器上显示数据,但有时要命名由于列表生成器异步运行,因此列表上可以交换。

我应该怎么做才能顺序显示来自Firestore的客户和产品名称?

public class CurrentOrders extends AppCompatActivity {
    private List<CurrentOrdersPOJO> currentOrdersPOJOList;
    private List<OrderPOJO> orderPOJOList;
    private FirebaseFirestore firebaseFirestore;
    private String DocId, Area, cname, pname;
    private OrderPOJO orderPOJO;
    private CurrentOrdersPOJO currentOrdersPOJO;
    private int count = -1, count1 = -1, i;
    private RecyclerView recyclerView;
    private List<String> customerName, productName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current_orders);

        //Current User Unique ID
        DocId = getIntent().getStringExtra("DocumentId");
        Area = getIntent().getStringExtra("Area");
        Log.w("ReachedCurrentOrders", "Doc Id: " + DocId + "\nArea: " + Area);

        currentOrdersPOJOList = new ArrayList<>();
        customerName = new ArrayList<String>();
        productName = new ArrayList<String>();
        orderPOJOList = new ArrayList<>();

        recyclerView = findViewById(R.id.activity_current_order_recyclerView);
        firebaseFirestore = FirebaseFirestore.getInstance();

        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

        firebaseFirestore.collection("order")
                .whereEqualTo("area", Area)
                .whereEqualTo("status", "active")
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(final QuerySnapshot queryDocumentSnapshots) {
                        if (!queryDocumentSnapshots.isEmpty()) {

                            for (final QueryDocumentSnapshot queryDocumentSnapshot : queryDocumentSnapshots) {
                                count++;
                            }

                            for (final QueryDocumentSnapshot queryDocumentSnapshot : queryDocumentSnapshots) {

                                orderPOJO = queryDocumentSnapshot.toObject(OrderPOJO.class);
                                orderPOJOList.add(orderPOJO);

                                Log.d("Tagging", "The Customer UID: " + orderPOJO.getC_uid() + "\nThe Product Doc ID: " + orderPOJO.getP_docid());

                                count1++;

                                if (count == count1) {
                                    getCustomerName();
                                }

                            }//endof for loop

                        } else {
                            Toast.makeText(CurrentOrders.this, "No Orders in Your Area", Toast.LENGTH_SHORT).show();
                            Log.d("CurrentOrder", "Exception Here");
                        }

                    }

                });
    }//endofOnCreate

    public void getCustomerName() {
        count1 = -1;
        //Getting Customer Name from ID
        for (i = 0; i <= count; i++) {
            firebaseFirestore.collection("customer").document(orderPOJOList.get(i).getC_uid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot.exists()) {

                        cname = documentSnapshot.getString("name");
                        customerName.add(cname);

                        count1++;

                        if (count1 == count) {
                            getProductName();
                        }

                    } else {
                        Log.d("CurrentOrders", "Exception Here" + documentSnapshot.exists());
                    }
                }
            });
        }
    }//end of function

    public void getProductName() {
        count1 = -1;
        //Product Getting Name
        for (i = 0; i <= count; i++) {
            firebaseFirestore.collection("product").document(orderPOJOList.get(i).getP_docid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot.exists()) {
                        pname = documentSnapshot.getString("name");
                        productName.add(pname);

                        count1++;

                        if (count1 == count) {
                            callAdapter();
                        }
                    } else {
                        Log.d("CurrentOrders", "Exception Here" + documentSnapshot.exists());
                    }
                }

            });
        }
    }//endofFunction

    public void callAdapter() {
        for (int i = 0; i <= count; i++) {

            currentOrdersPOJO = new CurrentOrdersPOJO(customerName.get(i), orderPOJOList.get(i).getComplete_address(),
                    productName.get(i), orderPOJOList.get(i).getQuantity(), orderPOJOList.get(i).getStatus(), orderPOJOList.get(i).getArea(), orderPOJOList.get(i).getO_date());

            currentOrdersPOJOList.add(currentOrdersPOJO);
        }
        recyclerView.setAdapter(new CurrentOrdersAdapter(currentOrdersPOJOList, CurrentOrders.this));
    }//endofFunction

}//endofclass

[活动列表的屏幕快照随时间变化] [1]

  [1]: https://i.stack.imgur.com/X48JF.jpg

2 个答案:

答案 0 :(得分:1)

在另一个thread上也提出了类似的问题,由于该方法本身是一个任务,因此您似乎可以同步返回数据,您可以尝试使用Taks.await(task)方法来等待该操作。操作结束,也许这就是您要寻找的答案。

答案 1 :(得分:0)

我已经通过使用上述@Ricardo提到的解决方案并通过使用Asynctask(Background Thread)将该解决方案与该解决方案组合来解决了此问题,因为它首先由于在Main UI Thread上调用Tasks.await(task)而给出了IllegalStateException。

因此,请使用: Aysnctask(背景线程)上的Tasks.await(任务)