将Firestore时间戳转换为日期数据类型 [英] Converting Firestore Timestamp to Date data type

查看:57
本文介绍了将Firestore时间戳转换为日期数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我刚刚将Firebase Pod更新到最新版本,并且在调试区域中,我收到一条消息,提示我必须将时间戳数据类型更新为Date(类似的事情,我实际上忘记了).

Few days ago, I just updated my firebase pod to the latest version, and in my debugging area, I got message that said that I have to update from timestamp data type to Date (something like that, I forget actually).

我还必须像下面那样更改数据库设置

and I also have to change the DB settings like below

let db = Firestore.firestore()
let settings = db.settings
settings.areTimestampsInSnapshotsEnabled = true
db.settings = settings

添加上面的代码后,Xcode中我的调试区域中的错误消息消失了.

after add the code above, the error message in my debugging area in Xcode disappears.

据我所知,我还必须将客户端中的数据类型从时间戳"更改为日期"数据类型,因为我在Xcode的调试区域中的错误消息已经消失,所以我没有更改它.

As far as I remember, I also had to change the data type in my client from Timestamp to Date data type, I haven't changed this because the error message in my debugging area in Xcode have disappeared.

结果是,我的应用程序中的日期不正确.

As a result, I get not correct Date in my app.

能否请您再次分享从时间戳到日期的转换步骤?因为据我所记得,我必须执行一些步骤.我在firebase文档中找不到它.

Could you please share again the conversion step from TimeStamp to Date ? because as far as I remember I had to do some steps to follow. I can't find it in the firebase documentation.

非常感谢:)

推荐答案

这是调试器发出的消息

存储在Firestore中的系统日期对象的行为将更改,并且您的应用可能会中断.要隐藏此警告并确保您的应用不会中断,您需要在调用任何其他Cloud Firestore方法之前向您的应用添加以下代码:

The behavior for system Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK. To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:

let db = Firestore.firestore()
let settings = db.settings
settings.areTimestampsInSnapshotsEnabled = true
db.settings = settings

通过此更改,存储在Cloud Firestore中的时间戳将作为Firebase时间戳对象而不是作为系统Date对象回读.因此,您还需要更新期望日期的代码,而不是期望时间戳的代码.例如:

With this change, timestamps stored in Cloud Firestore will be read back as Firebase Timestamp objects instead of as system Date objects. So you will also need to update code expecting a Date to instead expect a Timestamp. For example:

// old:
let date: Date = documentSnapshot.get("created_at") as! Date
// new:
let timestamp: Timestamp = documentSnapshot.get("created_at") as! Timestamp
let date: Date = timestamp.dateValue()

启用新行为时,请审核Date的所有现有用法.在将来的版本中,该行为将更改为新行为,因此,如果不执行这些步骤,则您的应用可能会崩溃.

Please audit all existing usages of Date when you enable the new behavior. In a future release, the behavior will be changed to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.

这篇关于将Firestore时间戳转换为日期数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆