Firebase / Swift queryOrder然后匹配值

时间:2016-06-09 20:10:48

标签: swift firebase firebase-realtime-database

昨天我问了一个被标记为重复的问题,当我更新问题时,它没有被标记。所以我在这里再问一次(根据stackoverflow' s推荐)。

我正在尝试按firebase中的多个值排序。我明白这是不可能的,但我得到了另一种语言的例子,这对于如何以正确的方式去做只有一半的帮助。在任何情况下,我都试着按照这里给出的例子Query based on multiple where clauses in firebase

这是我的firebase的结构

room
  -KJe22sduQMz1DIs_DH6
      allowedParticipants: 
        14
     createdBy: 
       "Mr Tester"
     members: 
        "nmeMYnnSatRch5qKPJKIe7jEOLy2"
    participating: 
       true
    status: 
       "seedling"
    theme: 
       "Cats"
    totalNumberOfMembers: 
       1

这是我试图开始工作的代码

ref.queryOrderedByChild("status").queryStartingAtValue("active").queryEndingAtValue("active").observeEventType(.Value) { (snapshot: FIRDataSnapshot) in

    let themeOfEvent = snapshot.value

    if themeOfEvent?.value == pickedTheme {

        print("foo")

    }
}

有人可以发一个有用的评论或答案来帮助我吗? 谢谢

1 个答案:

答案 0 :(得分:0)

我得到了帮助

这有效

func listOfPossibleCompetitionsFromFirebase(){

    let createdRoomRef = firebase.child("room")
    createdRoomRef.queryOrderedByChild("status").queryStartingAtValue("active").queryEndingAtValue("active").observeEventType(.Value) { (snapshot: FIRDataSnapshot) in

        var themeCount = 0

        self.listOfOpenComps.removeAll()
        if let tmp = snapshot.value as? [String:AnyObject] {
            let keys = tmp.keys

            for key in keys {

                if let roomDetails = (tmp[key] as? [String:AnyObject]) {

                if let themeOfEvent = roomDetails["theme"] as? String where themeOfEvent == pickedTheme {
                    themeCount += 1
                    self.listOfOpenComps.append(key)
                }

                }
            }
        }

        dispatch_async(dispatch_get_main_queue(), { 
            self.tableView.reloadData()
        })

        print("rooms count: \(themeCount)")



    }




}