【发布时间】: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