更新缓存并获取Android firestore云功能SnapshotListener中最新文档的字段

时间:2019-03-18 12:58:22

标签: java android google-cloud-firestore google-cloud-datastore google-cloud-functions

用户在诊所进行约会。我想获取最后一位用户的约会时间,该时间在我的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)...
      }

0 个答案:

没有答案