如果我使用 Firebase 数据库,我可以将服务器时间设置如下:
user.put("time", ServerValue.TIMESTAMP);
然后更新它。
但是如果我在FireStore 中使用相同的,它不会成功,而且我也不需要为了这个目的firebase-data-base
在我的gradle 中实现。
我可以看到ServerTimestamp.class
与FireStore相关,但我不知道如何使用它。
提前致谢。
根据文档,它会是这样的:
@ServerTimestamp Date time;
如果null
它将具有服务器生成的时间戳,那么您不需要为它设置值。
Ref:用于标记要填充服务器时间戳的日期字段的注释。如果正在写入的 POJO 包含 @ServerTimestamp 注释字段的 null,它将被服务器生成的时间戳替换。(https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/ServerTimestamp)
编辑:
或者map
直接使用类型对象时:
DocumentReference docRef = db.collection("objects").document("some-id");
// Update the timestamp field with the value from the server
Map<String,Object> updates = new HashMap<>();
updates.put("timestamp", FieldValue.serverTimestamp());
参考:https : //firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句