Firebase服务器时间戳为Double iOS [英] Firebase Server Timestamp to Double iOS

查看:56
本文介绍了Firebase服务器时间戳为Double iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ServerValue.timestamp()返回 [AnyHashable:Any] .如何将其转换为 Double ,所以我可以使用时间戳创建日期.

ServerValue.timestamp() returns [AnyHashable : Any]. How to convert it to Double, so I could create a date with the timestamp.

推荐答案

那不是Firebase时间戳的工作原理.

That's not exactly how the Firebase timestamp works.

它实际上是将时间戳记写入节点,但是直到写入后您才可以访问它.

What it actually does is writes a timestamp to a node but you don't have access to it until after the write.

要访问它,请将观察者附加到该节点,以便在写入时间戳时将其返回到快照中.

To get access to it, attach an observer to that node so when the timestamp is written, it will be returned in a snapshot.

所以首先我们定义一个变量

So first we define a var

let kFirebaseServerValueTimestamp = [".sv":"timestamp"]

然后将观察者附加到节点,以便在写入时间戳时将事件通知给我们

then attach an observer to a node so when the timestamp is written, we are notified of the event

func attachObserver() {

    let timestampRef = self.ref.child("timestamp")
    timestampRef.observe(.value, with: { snapshot in
        if snapshot.exists() {
             let ts = snapshot.value as! //Int? Double? String?
             print(ts)
        }
    })
}

和写出时间戳的函数,使上述观察者接收事件

and the function that writes out the timestamp, causing the above observer to receive the event

func doTimestamp() {
    let timestampRef = self.ref.child("timestamp")
    timestampRef.setValue(kFirebaseServerValueTimestamp)
}

希望有帮助-如果需要更多信息,请发表评论.

Hope that helps - if more info is needed, post a comment.

这篇关于Firebase服务器时间戳为Double iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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