如何按顺序获取Firebase快照中的子级列表?

时间:2018-12-19 06:21:08

标签: ios swift firebase firebase-realtime-database

我正在开发一个应用程序,该应用程序记录用户何时停止滚动运动,将滚动的偏移量和经过的时间附加到本地数组,然后在用户关闭应用程序时将滚动历史记录上载到Firebase。

Firebase中的数据在顶部存储有一个自动ID。然后,每个滚动偏移量和经过时间都存储在其父级下方的自己的自动ID子级中。在Firebase Web应用程序中,孩子的顺序正确。

我像这样从Firebase中提取数据:

ghostref.queryOrderedByKey().queryLimited(toLast: UInt(1)).observe(.childAdded, with: { (snapshot) in

       guard let ghostdict = snapshot.value as? [String:[String:String]] else {
            print("failure")
            return
        }

        var downloadedghostarray = [(cameray:Float, timeelapse:Double)]()

        for key in ghostdict.keys {
            downloadedghostarray.append((cameray: Float(ghostdict[key]!["cameray"]!)!, timeelapse: Double(ghostdict[key]!["timeelapse"]!)!))
        }
}

当我获得所需的数据时,它的顺序不正确。有什么方法可以按预期顺序拉动Firebase子级?也许我也可以通过键命令快照的子级?

编辑:以下是按所需顺序显示在Firebase网络应用中的数据:

enter image description here

这是使用上面的代码呈现的数组:

enter image description here

3 个答案:

答案 0 :(得分:0)

通过按关键字迭代节点字段并按关键字组织它们,可以有效地将列表中的元素随机化。基于哈希的字典/地图不能保证顺序得到维持。

您将不得不使用children迭代快照,(我相信)可以确保维持子级的顺序。此顺序应允许您将它们推入已确定顺序的另一个数组中。

答案 1 :(得分:0)

class func downloadAllMessages(forUserID: String, completion: @escaping ([Message]) -> Swift.Void, locationCompletion: @escaping (String) -> Swift.Void) {
    if let userID = Helper.shared.driverLoggedInDetails.detail?.userid {
        let currentUserID = "D\(userID)"

        dbReference.child("users").child(currentUserID).child("conversations_list").child(forUserID).observeSingleEvent(of: .value) { (snapshot) in
            if snapshot.exists() {
                let data = snapshot.value as! [String: Any]
                let location = data["location"]!
                locationCompletion(location as! String)

                dbReference.child("messages").child(location as! String).observe(.childAdded, with: { (snap) in
                    if snap.exists() {

                        let receivedMessages = snap.value as! [String: Any]
                        var messages1 = [Message]()
                        let type = MessageType.text

                        let text = (receivedMessages as NSDictionary).object(forKey: "text") as? String
                        let mmText = (receivedMessages as NSDictionary).object(forKey: "mmText") as? String
                        let messageType = (receivedMessages as NSDictionary).object(forKey: "messageType") as? Int

                        let fromID = (receivedMessages as NSDictionary).object(forKey: "senderId")as? String
                        let timestamp = (receivedMessages as NSDictionary).object(forKey: "timeStamp")as? Int
                        let isRead = (receivedMessages as NSDictionary).object(forKey: "read")as? Bool
                        let isvisible  = UserDefaults.standard.object(forKey: "chatwindow") as? Bool
                        if fromID != currentUserID, isvisible ?? false {
                            dbReference.child("messages").child(location as? String ?? "").child(snap.key).child("read").setValue(true)
                        }

                        if fromID == currentUserID {
                            let message = Message.init(type: type, textEn: text ?? "", textMM: mmText ?? "", owner: .receiver, timestamp: timestamp ?? 0, isRead: isRead ?? false, isSuggested: messageType == -1 ? true : false)
                            messages1.append(message)

                        } else {
                            let message = Message.init(type: type, textEn: text ?? "", textMM: mmText ?? "", owner: .sender, timestamp: timestamp ?? 0, isRead: isRead ?? false, isSuggested: messageType == -1 ? true : false)
                            messages1.append(message)
                        }

                        completion(messages1)
                    }else {
                        // LoadingIndicator.shared.hide()
                        completion([])
                    }
                })
                // LoadingIndicator.shared.hide()
                completion([])
            }
        }
    }
}

答案 2 :(得分:0)

U可以通过在1..n中的firebase文档中添加一个数字字段来获得,以便您可以使用基于升/降的查询。结果将是您的预期结果