【问题标题】:How to add server time using FirebaseStore timestamp?如何使用 FirebaseStore 时间戳添加服务器时间?
【发布时间】:2017-11-08 06:12:45
【问题描述】:

如果我使用 Firebase 数据库,我可以如下设置服务器时间:

user.put("time", ServerValue.TIMESTAMP);

然后更新它。

但如果我在 FireStore 中使用相同的,它不会成功,而且我不需要在我的 gradle 中实现firebase-data-base 仅用于此目的.

我可以看到有ServerTimestamp.classFireStore相关,但我不知道如何使用。

提前致谢。

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    根据文档,它会是这样的:

    @ServerTimestamp Date time;
    

    如果null 它将具有服务器生成的时间戳,因此您无需为其设置值。

    参考: 用于标记要使用服务器时间戳填充的日期字段的注释。如果正在写入的 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

    【讨论】:

    • 谢谢,但@ServerTimestamp Date time; 似乎没有合法的语法。
    • 只在使用类时使用。如果您使用地图,请查看我的更新。
    • 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多