如何快速创建负 Firebase 时间戳 [英] How to create negative Firebase timestamp in swift

查看:21
本文介绍了如何快速创建负 Firebase 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在开发的 iOS 应用程序,我需要按降序获取消息,即最新消息首先出现,然后是第二个最新消息等.

For an iOS app I am working on, I need to fetch messages in descending order i.e the latest message comes first, followed by second newest message etc.

从查看其他 SO 答案和研究来看,对于我的情况似乎最好的方法是创建一个负时间戳,然后将其作为消息的额外属性保存到数据库中.

From looking at other SO answers and research it seems that the best approach for my situation is to create a negative timestamp and then persist that to the database as an extra property to messages.

然后我将使用 queryOrderedByChild('negativeTimestamp') 来获取 observeSingleEvent 中的消息,然后有一个 childAdded 观察者来处理一旦初始调用被发送的消息制作.

I will then use queryOrderedByChild('negativeTimestamp') to fetch the messages in a observeSingleEvent and then have a childAdded observer to handle messages which are sent once initial calls are made.

在 firebase 文档 中,它说我可以从此代码片段中获取服务器时间戳值 firebase.database.ServerValue.TIMESTAMP

In the firebase documentation it says I can get the server timestamp value from this snippet of code firebase.database.ServerValue.TIMESTAMP

我如何为 Swift 3 编写这个?

How do I write this for Swift 3?

推荐答案

首先,请参阅评论中的链接答案.该答案依赖于客户端生成一个时间戳,该时间戳为负值并写入 Firebase.

First, see the linked answer in the comments. That answer relies on the client to generate a timestamp that's made negative and written to Firebase.

如果您想让 Firebase 生成时间戳,也可以使用这个小巧的 Firebase 结构和一段代码来完成.

If you want to have Firebase generate a timestamp, that can be done as well with this little snappy Firebase structure and piece of code.

先来看看结构

root
  parent_node
    -Y8j8a8jsjd0adas
       time_stamp: -1492030228007
  timestamp: 1492030228007

接下来,一些代码来创建和使用该结构:

Next, some code to create and work with that structure:

定义一个我们可以在我们的类中使用的引用 Firebase 时间戳的变量

Define a var we can use within our class that references the Firebase time stamp

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

以及一个向时间戳节点添加观察者的函数:

and a function that adds an observer to the timestamp node:

func attachObserver() {

    let timestampRef = self.ref.child("timestamp")
    let parentNodeRef = self.ref.child("parent_node")
    var count = 0

    timestampRef.observe(.value, with: { snapshot in

        if snapshot.exists() {
            count += 1
            if count > 1 {
                let ts = snapshot.value as! Int
                let neg_ts = -ts
                let childNodeRef = parentNodeRef.childByAutoId()
                let childRef = childNodeRef.child("time_stamp")
                childRef.setValue(neg_ts)
                count = 0
            }
        }
    })

还有一个写出时间戳的函数,因此导致观察者触发,根据 Firebase 时间戳在 parent_node 内创建子节点

And a function that writes out a timestamp, therefore causing the observer to fire which creates child nodes within the parent_node based on the Firebase time stamp

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

这是纲要.

在 attachObserver 函数中,我们将观察者附加到时间戳节点 - 该节点可能存在也可能不存在,但如果不存在,它将被创建 - 继续阅读.每当时间戳节点中发生事件时,都会调用闭包中的代码.

In the attachObserver function, we attach an observer to the timestamp node - that node may or may not exist but if it doesn't it will be created - read on. The code in the closure is called any time an event occurs in the timestamp node.

当 doTimestamp 函数被调用时,它会创建一个时间戳并将其写入时间戳节点,然后触发我们在 attachObserver 中附加的观察者.

When the doTimestamp function is called, it creates and writes a timestamp to the timestamp node, which then fires the observer we attached in attachObserver.

observe 闭包中的代码执行以下操作:

The code in the observe closure does the following:

确保快照包含某些内容,如果包含,则增加一个计数器(稍后会详细介绍).如果计数器大于 1,则从快照中以整数形式获取时间戳.然后,创建它的负值并将其作为 parent_node 的子节点写回 Firebase.

Make sure the snapshot contains something, and if it does, increment a counter (more on that in a bit). If the counter is greater than 1 get the timestamp as an integer from the snapshot. Then, create it's negative and write it back out to Firebase as a child of parent_node.

这将如何适用于任何时候您想要使用 Firebase 生成的时间戳为子节点添加时间戳但反向加载/排序的负值 - 这说明了 OP 问题.

How this would apply would be anytime you want to timestamp a child node with a Firebase generated timestamp but negative value for reverse loading/sorting - which speaks to the OP question.

这里的问题是当这种情况发生时

The gotcha here is that when this happens

    timestampRef.setValue(kFirebaseServerValueTimestamp)

它实际上向节点写入了两次,这将导致更接近的代码被调用两次.

It actually writes twice to the node, which would cause the code in the closer to be called twice.

也许 Firebaser 可以解释这一点,但我们需要忽略第一个事件并捕获第二个事件,即实际时间戳.

Maybe a Firebaser can explain that, but we need to ignore the first event and capture the second, which is the actual timestamp.

所以第一个事件将导致观察者更接近触发,使得计数 = 1,由于 if 语句,它将被忽略.

So the first event will cause the observer closer to fire, making count = 1, which will be ignored due to the if statement.

然后触发第二个事件,其中包含实际时间戳,这就是我们用来制作否定并写入 Firebase 的内容.

Then the second event fires, which contains the actual timestamp, and that's what we use to make negative and write out to Firebase.

希望这对 OP 和评论者有所帮助.

Hope this helps the OP and the commenters.

这篇关于如何快速创建负 Firebase 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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