在Firebase中抓取孩子的所有子节点?

时间:2015-07-11 01:38:08

标签: ios swift firebase children

我正在努力解决我想在Firebase中做的事情。我似乎无法弄清楚如何做到这一点。我已经找到了我想用Javascript方法做的答案,但似乎无法用iOS做到这一点。我所拥有的是一组看起来像这样的数据:

events
  -JFSDFHdsf89498432
    eventCreator: "Stephen"
    eventCreatorId: 1764137
  -JOeDFJHFDSHJ14312
    eventCreator: "puf"
    eventCreatorId: 892312
event_attendees
  -JFSDFHdsf89498432
    -JSAJKAS75478
      name: "Johnny Appleseed"
      objectID: "1"
    -JSAJKAS75412
      name: "use1871869"
      objectID: "2"
  -JOeDFJHFDSHJ14312
    -JaAasdhj1382
      name: "Frank van Puffelen"
      objectID: "3"
    -Jo1asd138921
      name: "use1871869"
      objectID: "4"

我想要做的是抓住event_attendees引用中每个孩子的孩子的信息,并将该信息存储到数据库中。例如,我想抓住event_attendees - > JFSDFHdsf89498432 - > JSAJKAS75478 - > name: Johnny AppleseedobjectID: "1"并将此信息存储到某种字典中。我想为JFSDFHdsf89498432孩子中的每个孩子做这种模式。最后,我的数组看起来像这样:

Array = [
 {name: "Johnny Appleseed", objectID: "1"},
 {name: "use1871869", objectID: "2"}
]

执行此操作后,该函数将通过完成处理程序返回此数组,从而完成该功能。但是,我似乎找不到办法做到这一点。以下是我的尝试:

func getAttendees(child: String, completion: (result: Bool, name: String?, objectID: String?) -> Void){
    //Get event attendees of particular event
    var attendeesReference = self.eventAttendeesRef.childByAppendingPath(child)
    var newArrayOfAttendees = [AnyObject]()

    println("Loading event attendees")
    //Get all event attendees

    attendeesReference.observeEventType(FEventType.Value, withBlock: { (snapshot) -> Void in

        for i in snapshot.children {
            newArrayOfAttendees.append(i)
            //This returns an entire snapshot which I'm not sure how to parse that looks like so:
            /*
                Snap (JFSDFHdsf89498432) {
                   name = "Johnny Appleseed";
                   objectID = "1";
                  }
            */
            //I also cannot seem to access i.value
            //because i.value gives me an error saying 
            //Could not find member 'init'
            //But the JavaScript version says you can do that via: https://www.firebase.com/docs/web/api/datasnapshot/foreach.html
            //But i.key returns me JFSDFHdsf89498432 which is correct.
        }
        completion(result: true, attendees: newArrayOfAttendees)
        }) { (error) -> Void in
            println(error.description)
    }

而我似乎无法弄清楚如何获取子节点的所有子节点,将其存储到数组中,并通过完成处理程序传递该数组。如果有人能帮助我,这将是伟大的。非常感谢。

0 个答案:

没有答案