java - Android firestore 云函数 SnapshotListener 更新缓存并获取最新文档的字段
问题描述
用户在诊所预约。我想获取最后一个用户的预约时间,该时间位于我的 firestore 数据库的顶部文档中。我能够得到它,但是如果我删除顶部文档并进行另一次约会,它将使用已删除文档的时间而不是新的顶部文档。如果有更好的方法请分享。
Collection col = firestore.collection("Clinic Name");
//Get 1 document the top one
Query query = col.orderBy("Time",Query.Direction.DESCENDING).limit(1);
query.addSnapshotListener(MetadataChanges.INCLUDE, new
EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots,
@javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null) {
return;
}
if (queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()) {
for (DocumentSnapshot dc : queryDocumentSnapshots) {
String prevTime = dc.getString("Time");
//Add the data
AddAppointment(prevTime);
}
}
//Function that add the data
void AddAppointment(String time){
if(time.isEmpty){
time = "08:00:00";
}
LocalTime localTime = LocalTime.parse(time);
localTime = String.valueOf(localTime.plusMinutes(8));
Map<String, Object> data = new HashMap<>();
data.put("Time",localTime);
//more data ...
//Adding the document
firebase.collection("ClinicName").document("userId").set(data)...
}
解决方案
推荐阅读
- reactjs - 反应管理员未满足的对等依赖项警告
- r - R中的缺失值Jarque Bera测试
- jquery - 尝试在 Highcharts 中使用 jquery 制作的数组中的数组
- vue.js - 使用外部组件时出现 VueJS 错误
- python-2.7 - 如何在不发生无限循环的情况下从此 python 代码中消除偶数?
- mysql - Yii2 mysql 处理更大的数据
- linux - CentOS Apache 服务器正在 Chrome 浏览器中运行和访问网站
- postgresql - 如何限制只有 1 行在 postgres 中更新?
- fluent-ui - fluentui/react-northstar 版本升级是否向后兼容?
- python - 在 Python 中使用 Pandas,如果单词已经出现了 3 次,则删除数据框中的行