如何在本地合并任务中检索Firestore子集合文档

时间:2020-02-25 21:47:29

标签: java android firebase google-cloud-firestore

首先,我有一些产品,其中一些具有大小等变体,而其他没有变体,我需要从所有产品中仅检索实时产品并添加分页。

合并任务的问题运行正常,但在其中查询子集合文档(如果产品具有变体)不起作用

firestore结构

product without variants product with variant variant details

 private void getStoreLiveProducts(String storeId) {
    final int limit = 9;

    Task task1, task;
    if (lastLiveProductRef == null) {
        msvLiveProducts.setViewState(MultiStateView.ViewState.LOADING);
        task1 = FirebaseFirestore.getInstance()
                .collection("Products")
                .whereEqualTo("storeId", storeId)
                .whereEqualTo("status", "live")
                .limit(limit)
                .get();
        task = FirebaseFirestore.getInstance()
                .collection("Products")
                .whereEqualTo("storeId", storeId)
                .whereEqualTo("hasVariant", true)
                .limit(limit)
                .get();
    } else {
        ppMoreProducts.setVisibility(View.VISIBLE);
        task1 = FirebaseFirestore.getInstance()
                .collection("Products")
                .whereEqualTo("storeId", storeId)
                .whereEqualTo("status", "live")
                .limit(limit)
                .startAfter(lastLiveProductRef)
                .get();
        task = FirebaseFirestore.getInstance()
                .collection("Products")
                .whereEqualTo("storeId", storeId)
                .whereEqualTo("hasVariant", true)
                .limit(limit)
                .startAfter(lastLiveProductRef)
                .get();
    }

    Task<List<QuerySnapshot>> allSuccess = Tasks.whenAllSuccess(task1, task);

    allSuccess.addOnSuccessListener(new OnSuccessListener<List<QuerySnapshot>>() {
        @Override
        public void onSuccess(List<QuerySnapshot> querySnapshots) {
            for (QuerySnapshot queryDocumentSnapshots : querySnapshots) {
                for (final DocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                    if (ppMoreProducts.getVisibility() == View.VISIBLE)
                        ppMoreProducts.setVisibility(View.GONE);
                    msvLiveProducts.setViewState(MultiStateView.ViewState.CONTENT);
                    if (documentSnapshot.get("hasVariant") != null) {
                        boolean hasVariant = (boolean) documentSnapshot.get("hasVariant");
                        if (!hasVariant) {
                            Product product = documentSnapshot.toObject(Product.class);
                            liveProducts.add(product);
                            productAdapter.notifyDataSetChanged();
                        }
                    } else {
                        Log.e( "onSuccess: ", documentSnapshot.getId()+"");
                        documentSnapshot
                                .getReference()
                                .collection("Variants")
                                .whereEqualTo("status", "live")
                                .get()
                                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                                    @Override
                                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                                        for (DocumentSnapshot snapshot : queryDocumentSnapshots.getDocuments()) {
                                            Log.e( "snapshot: ", snapshot.getId()+"");
                                        }
                                    }
                                });
                    }
                }
                if (queryDocumentSnapshots.getDocuments().size() < limit)
                    lastItemReached = true;
                lastLiveProductRef = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.getDocuments().size() - 1);
            }
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.e("onFailure: ", e.getMessage() + "");
        }
    });
}

0 个答案:

没有答案