如何在Swift中从Firebase追加过滤数据

时间:2018-03-21 06:16:28

标签: ios swift firebase firebase-realtime-database

enter image description here enter image description here enter image description here enter image description here请在下面找到我的代码。我们如何在Firebase上的数组中附加过滤数据?

 var childrenList = [DatabaseList]()
 let ref = Database.database().reference(withPath: "Messages")
        let query = ref.queryOrdered(byChild: "VideoID").queryEqual(toValue: "12345").observe(.value, with: { (snapshot) in
            for childSnapshot in snapshot.children{
                print(childSnapshot)
                self.childrenList.append(snapshot)
            }
        })
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }

2 个答案:

答案 0 :(得分:2)

   let ref = Database.database().reference(withPath: "Messages")
   let query = ref.queryOrdered(byChild: "VideoID").queryEqual(toValue: "12345").observe(.value, with: { (snapshot) in
     print(snapshot)

    for (childSnapshotId, childSnapshotValue) in snapshot {
       if let dataListDict = childSnapshotValue as? [String: AnyObject] {
          //Init you newModel with the dataListDict here
          let newModel = DatabaseList(dict: dataListDict)
         print(childSnapshot)
         self.childrenList.append(newModel)
       }
    }

    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
})


   class DatabaseList : NSObject { 
    var messageBody : String? 
    var name : String? 
    var videoID : String? 
   init(dict: [String: AnyObject]) { 
       messageBody = dict["MessageBody"]
       name = dict["Name"]
       videoID = dict["videoID"]
   } 
 }

答案 1 :(得分:1)

您的查询是正确的,但在完成阻止时几乎没有错误。

self.childrenList.append(snapshot)快照是DataSnapshot而不是DatabaseList的实例,因此您无法像这样追加它。

for childSnapshot in snapshot.children {

     /// childSnapshot is an instance of DataSnapshot not a dictionary but its value will be
     guard let data = (childSnapshot as! DataSnapshot).value else {continue}
     let dataDict = data as! Dictionary<String, Any>

     /// Initializing the new object of DatabaseList and passing the values from data
     let list: DatabaseList = DatabaseList()
     list.messageBody = dataDict["MessageBody"] as? String
     list.name = dataDict["Name"] as? String
     list.videoID = dataDict["VideoID"] as? String

     /// This is correct, and now you can append it to your array.
     childrenList.append(list)
 }

除此之外,您将不得不重新加载整块内的tableView而不是块下方,因为这是一个异步请求,数据将在稍后出现。

检查数据存在总是更好。 snapshot.exists()

如果您只想获取一次数据,那么还有一个建议,请不要使用.observe使用.observeSingleEvent。每次此节点发生任何更改时,.observe都会触发该块。

以下是完整的代码段。

let query = ref.queryOrdered(byChild: "VideoID").queryEqual(toValue: "12345").observe(.value, with: { (snapshot) in

      if !snapshot.exists() {
         // Data doesn't exist
         return
      }

      for childSnapshot in snapshot.children {
           guard let data = (childSnapshot as! DataSnapshot).value else {continue}
           let dataDict = data as! Dictionary<String, Any>

           let list: DatabaseList = DatabaseList()
           list.messageBody = dataDict["MessageBody"] as? String
           list.name = dataDict["Name"] as? String
           list.videoID = dataDict["VideoID"] as? String
           childrenList.append(list)
       }

       /// Reload your tableView here
       DispatchQueue.main.async {
           self.tableView.reloadData()
       }
 })

期待下面的课程模式:

class DatabaseList: NSObject {
    var messageBody: String?
    var name: String?
    var videoID: String?
}