菜鸟教程小白 发表于 2022-12-11 19:51:41

ios - Firebase 服务器时间戳使 iOS 翻倍


                                            <p><p><code>ServerValue.timestamp()</code> 返回 <code></code>。如何将其转换为 <code>Double</code>,以便我可以创建带有时间戳的日期。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>Firebase 时间戳的工作原理并非如此。</p>

<p>它实际上所做的是将时间戳写入节点,但在写入之后您才能访问它。</p>

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

<p>所以首先我们定义一个变量</p>

<pre><code>let kFirebaseServerValueTimestamp = [&#34;.sv&#34;:&#34;timestamp&#34;]
</code></pre>

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

<pre><code>func attachObserver() {

    let timestampRef = self.ref.child(&#34;timestamp&#34;)
    timestampRef.observe(.value, with: { snapshot in
      if snapshot.exists() {
             let ts = snapshot.value as! //Int? Double? String?
             print(ts)
      }
    })
}
</code></pre>

<p>以及写出时间戳的函数,使上面的观察者接收到事件</p>

<pre><code>func doTimestamp() {
    let timestampRef = self.ref.child(&#34;timestamp&#34;)
    timestampRef.setValue(kFirebaseServerValueTimestamp)
}
</code></pre>

<p>希望对您有所帮助 - 如果需要更多信息,请发表评论。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Firebase 服务器时间戳使 iOS 翻倍,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44527008/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44527008/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Firebase 服务器时间戳使 iOS 翻倍