如何按时间戳排序消息

时间:2017-09-19 11:14:15

标签: ios swift firebase firebase-realtime-database

我希望按创建日期(即时间戳)获取所有邮件。

我的数据结构如下:

messages
  XRJONcahNUgeNrfkH3cmITJt3409 (auto uid of owner - user)
    -KoOSxA2Iajk3kv20U_p (automatic uid generated by Firebase)
      message: "Hi"
      senderId: "X8Oxg5J9Imb12C1iYBXJT9P48909"
      senderName: "Dave"
      timestamp: 1505813320526 (automatic timestamp)
    -GdOSxR2Iajk3kv20U_p --- I NEED TO SORT THESE UIDS
      message: "Hello"
      senderId: "G7Oxg5J9Imb12C1iYBXJT9P48909"
      senderName: "Frank"
      timestamp: 1505813321024 (automatic timestamp)

我收到这样的所有消息:

database.child("messages").child(ownersId).observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot)
            if let messagesDict = snapshot.value as? [String:AnyObject] {
                for (_,message) in messagesDict {
                    self.messages.append((message["message"] as? String)!)
                    self.users.append((message["senderName"] as? String)!)
                    self.timestamps.append((message["timestamp"] as? IntMax)!)
                }
            }
            completionHandler(true)

        }) { (error) in
            print(error.localizedDescription)
            completionHandler(false)
        }

所以问题是,如何才能获得从最旧到最新订购​​的所有消息?我不知道如何在这种特定情况下使用排序方法(queryOrderedByKey(),queryOrderedByValue()或queryOrdered(byChild:“timestamp”).Firebase以随机顺序向我发送数据(我不知道它是否是随机的,但它是没有按日期排序。)

2 个答案:

答案 0 :(得分:2)

您使用database.queryOrdered代替database.child,如下所示:

database.queryOrdered(byChild: "messages"). observeSingleEvent(.value, with: { (snapshot) in
    print("\(snapshot.value)")
}

但您也可以像今天一样获取数据,然后对列表self.timestamps进行排序:

self.timestamps.sorted(by: { $0 > $1 })

<强>更新
创建模型并将属性附加到该模型,然后追加到列表并在此之后对该列表进行排序。像这样:

struct Model {
    let message: String?
    let senderId: String?
    let senderName: String?
    let timestamp: IntMax

    init(message: String?, senderId: String?, senderName: String?, timestamp: IntMax?) {
        self.message = message
        self.senderId = senderId
        self.senderName = senderName
        self.timestamp = timestamp
    }
}

var models = [Model]()
models.sorted(by: { $0.0.timestamp > $0.1.timestamp })

更新2:
要添加模型,请查看以下代码示例:

database.child("messages").child(ownersId).observeSingleEvent(of: .value, with: { (snapshot) in
    print(snapshot)
    var models = [Model]()
    if let messagesDict = snapshot.value as? [String:AnyObject] {
        for (_,message) in messagesDict {
            if let message = message["message"] as? String, let senderName = message["senderName"] as? String, let timestamp = message["timestamp"] as? IntMax, let senderId = message["senderId"] as? String {
                let model = Model(message: message, senderId: senderId, senderName: senderName, timestamp: timestamp)
                models.append(model)
            }
        }
        models.sorted(by: { $0.0.timestamp > $0.1.timestamp })
    }
    completionHandler(true)

}) { (error) in
    print(error.localizedDescription)
    completionHandler(false)
}

答案 1 :(得分:1)

正如Rashwan所说,您需要致电Alternatives以正确的顺序获取商品。然后循环A以确保您也以正确的顺序处理它们。

所以:

queryOrderedByChild()

回调中的重要内容是不将整个快照转换为字典,因为字典不会维护项目的顺序。