【问题标题】:Android - Query all documents from subcollection as objects - Firestore realtime databaseAndroid - 将子集合中的所有文档作为对象查询 - Firestore 实时数据库
【发布时间】:2019-05-14 23:39:09
【问题描述】:

我有一个模型类 Appointment,其类型为字段,我需要在 Firestore 实时数据库中从 subCollection 监听并获取所有文档 ,上传逻辑完美运行,但由于这实际上是我第一次潜入 Firestore,我在获取数据时遇到了一些麻烦。

拿到文件后需要按类型分开(预约类型_A,预约类型_B)

这是我目前所获得的,但我似乎无法超越!

附言。以下代码主要基于 Firebase 文档!

@Override
public void onStart() {
    super.onStart();
    //get all appointments
    mFirestore.collection("reporters").document(mUser.getUid()).collection("appointments")
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                    mFirestore.collection("reporters").document().collection("appointments")
                            .whereEqualTo("type", "typeB")
                            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                                @Override
                                public void onEvent(@Nullable QuerySnapshot value,
                                                    @Nullable FirebaseFirestoreException e) {
                                    if (e != null) {
                                        Toast.makeText(getActivity(), "listen failed", Toast.LENGTH_SHORT).show();
                                        return;
                                    }

                                    for (QueryDocumentSnapshot doc : value) {
                                        if (doc.get("name") != null) {
                                            v_appointmentsList.add(doc.toObject(Appointment.class));
                                        }
                                    }
                                }
                            });
                    mFirestore.collection("reporters").document().collection("appointments")
                            .whereEqualTo("type", "typeA")
                            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                                @Override
                                public void onEvent(@Nullable QuerySnapshot value,
                                                    @Nullable FirebaseFirestoreException e) {
                                    if (e != null) {
                                        Toast.makeText(getActivity(), "listen failed", Toast.LENGTH_SHORT).show();
                                        return;
                                    }

                                    for (QueryDocumentSnapshot doc : value) {
                                        if (doc.get("name") != null) {
                                            p_appointmentsList.add(doc.toObject(Appointment.class));
                                        }
                                    }
                                }
                            });
                    //get all docs
                    //....
                    //sort by date [max -> min || min -> max]
                    //...
                    //get index [0] || index [last]
                    //...
                    //if it is vax then affect to variables v_
                    //if it is ped then affect to variables p_
                }
            });
}

【问题讨论】:

  • 请密切关注产品标签:Realtime Database 和 Cloud Firestore 是两个不同的数据库。虽然两者都是 Firebase 的一部分,但它们具有不同的 API 和不同的标签。您应该很少需要同时标记问题。
  • 请将您的数据库结构添加为屏幕截图并回复@AlexMamo
  • @AlexMamo 你的意思是来自 Firebase 控制台的屏幕截图?我不太明白你的意思
  • 是的,来自 Firebase 控制台的屏幕截图。

标签: java android firebase google-cloud-firestore


【解决方案1】:

对于您的情况,我建议您执行以下操作(我的答案基于下面的评论部分),对于使用实时数据库的类似情况并不重要,您可以尝试使用此方法:

//colRef is a reference/path to your collection
//filter by type with .whereEqualTo("field", "type")


colRef.whereEqualTo("type", "yourType")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
                            //add to your arraylist
                        }
                    } else {
                        Toast.makeText(getContext(), "fail to get docs", Toast.LENGTH_SHORT).show();
                    }
                }
            });


//repeat the process for the second type
//if you have more types I suggest using another approach

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 2021-09-17
    • 2021-09-25
    • 1970-01-01
    • 2020-07-26
    • 2021-04-08
    • 2018-07-01
    • 2021-07-12
    • 2018-06-19
    相关资源
    最近更新 更多