繁体   English   中英

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

[英]How can I get a list of children in a Firebase snapshot in order?

我正在开发一个应用程序,该应用程序记录用户何时停止滚动运动,将滚动的偏移量和经过的时间附加到本地数组,然后在用户关闭应用程序时将滚动历史记录上载到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 Web应用程序中的数据:

在此处输入图片说明

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

在此处输入图片说明

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

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

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([])
            }
        }
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM