从Firebase检索数据时崩溃

时间:2017-11-12 11:39:56

标签: swift firebase

我的代码

    let ref = Database.database().reference().child("PostInfo")
    let query = ref.queryOrdered(byChild: "post_title").queryEqual(toValue: self.retrieve_title)
    query.observeSingleEvent(of: .value) { (snapshot) in
       print((snapshot.childSnapshot(forPath: "status").value as? String)!)
    }
}

jSon Data

     {"PostInfo":{
   "-KyjkkEAZeHLjdRLg20w" : {
       "postImage" : "https://firebasestorage.googleapis.com/v0/b/hobbyquest-ee18d.appspot.com/o/8C40BA04-6D8D-4A23-B8BB-E1B3AC64E66F.png?alt=media&token=3f0f10e3-a64b-4187-9259-3c25bfc4a9e5",
       "post_title" : "hahahahah",
       "status" : "a banana an",
       "threadForHobby" : "Bowling",
       "userID" : "ccuvHt6feYVIO6GUXKo3OpO6VUn2"}
       }

我正在尝试从firebase获取状态数据,但应用程序一直在崩溃。请帮忙!

2 个答案:

答案 0 :(得分:2)

问题在于这一行

(snapshot.childSnapshot(forPath: "status").value as? String)!

您的代码正在按.value读取数据。

.value返回给定节点中的所有节点,您需要迭代它们。例如。假设我们的数据库如下所示,您正在查询post_title为title_0的帖子

PostInfo
  post_0
    postImage = "www.thing.com"
    status = "status_0"
    post_title = "title_0"
  post_1
    postImage = "www.yipee.com"
    status = "status_1"
    post_title = "title_1"
  post_2
    postImage = "www.dude.com"
    status = "status_2"
    post_title = "title_0"

运行查询时,将返回post_0和post_2,因为它们都有title_0

您需要遍历快照才能获得结果。

    let ref = self.ref.child("PostInfo")
    let query = ref.queryOrdered(byChild: "post_title").queryEqual(toValue: "title_0")
    query.observeSingleEvent(of: .value) { (snapshot) in
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let x = (snap.childSnapshot(forPath: "status").value as? String)!
            print(x)
        }
    }

如果您注意到 - 您的代码行在此方案中有效,因为它将每个子项检查为单独的快照。

另一方面,如果您只想返回第一个匹配项,则可以使用.childAdded,它将只返回一个单独的节点:

    let ref = self.ref.child("PostInfo")
    let query = ref.queryOrdered(byChild: "post_title").queryEqual(toValue: "title_0")
    query.observeSingleEvent(of: .childAdded) { (snapshot) in
            let x = (snapshot.childSnapshot(forPath: "status").value as? String)!
            print(x)
    }

答案 1 :(得分:1)

我不知道为什么,但你可以采用不同的方式,但改变数据的布局。

数据如下:

{"PostInfo":{
   "searchidentifier" : { //whatever identifier you want, I think you're trying to use the post title
          "object" : {
              "postImage" : "https://firebasestorage.googleapis.com/v0/b/hobbyquest-ee18d.appspot.com/o/8C40BA04-6D8D-4A23-B8BB-E1B3AC64E66F.png?alt=media&token=3f0f10e3-a64b-4187-9259-3c25bfc4a9e5",
              "post_title" : "hahahahah",
              "status" : "a banana an",
              "threadForHobby" : "Bowling",
              "userID" : "ccuvHt6feYVIO6GUXKo3OpO6VUn2"}
   }
 }

你会像这样检索你的数据:

let ref = Database.database().reference().child("PostInfo").child("\(self.retrieve_title)")

ref.observe(.value, with: { (snapshot) in
     if snapshot.childrenCount > 0 {
           for classes in snapshot.children.allObjects as![FIRDataSnapshot] {
               let classesObject = classes.value as? [String: AnyObject]
               let postImage = classesObject?["postimage"]
               //Retrieve all the other objects here as well
            }
      }
})