【问题标题】:Is it normal for timestamp to be saved into Firestore as "timestamp"?时间戳作为“时间戳”保存到 Firestore 是否正常?
【发布时间】:2018-06-22 09:09:23
【问题描述】:

我正在保存服务器值时间戳,如下所示:

let data : [String: Any] = ["userId": 9,
                            "name": "Elon Musk",
                            "timeCreated": ServerValue.timestamp()]

 let doc = firestore.collection("orders").document()
 doc.setData(data){ (error) in

 }

在查看 Firestore 时,虽然我看到了这个:

这正常吗?不应该有一个 long 包含自 Unix 纪元以来的毫秒数吗?

【问题讨论】:

  • 你的ServerValue.timestamp()函数是什么样的?
  • @creeperspeak,它与 Firebase iOS SDK 附带的相同。 ServerValue.timestamp()[AnyHashable : Any]

标签: swift firebase unix-timestamp google-cloud-firestore


【解决方案1】:

不,这似乎不正常。这是我使用 ServerValue.timestamp() 时得到的。

【讨论】:

    【解决方案2】:

    您正在使用服务器时间戳的实时数据库概念,即ServerValue.timestamp。它本质上是一个包含您显示的值的字典:{'.sv': 'timestamp'}。这与 Firestore 不兼容。

    相反,您想使用 Firestore 的服务器时间戳概念,即 FieldValue.serverTimestamp()。另请参阅documentation here 中的最后一个示例。

    【讨论】:

    • 如何从服务器中检索它?我的意思是,如果我将对象从 Firestore 映射到我的 Android 项目中的 POJO,例如,timeCreated 的类型应该是什么?我试过var timeCreated: FieldValue? = null 但它不起作用,因为 FieldValue 似乎没有时间戳的设置器
    • 它将是一个即将退出的数字,以自纪元以​​来的毫秒数表示。
    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多