1

I am struggling with something I want to do in Firebase. I can't seem to figure out how to do this. I've found the answer of I want to do with a Javascript-approach but can't seem to do this with iOS. What I have is a set of data that looks something like this:

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"

What I want to do is grab every child's child's information within the event_attendees reference and store that information into a database. For example, I want to grab event_attendees -> JFSDFHdsf89498432 -> JSAJKAS75478 -> name: Johnny Appleseed and objectID: "1" and store this information into some sort of dictionary. I want to do this pattern for every child within the JFSDFHdsf89498432 child. At the end, my array would look something like this:

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

Upon doing so, the function would return this array via a completion handler, thus finishing the function. However, I can't seem to find a way to do this. The following is my attempt:

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)
    }

And I just can't seem to figure out how to get all the children of a child node, store that into an array, and pass that array through a completion handler. If anyone could help me out that would be great. Thank you so much.

user1871869
  • 3,317
  • 13
  • 56
  • 106
  • Please don't use generic terms like "child" when you have something meaningful, like "event" and "attendee". So instead of "I want all the child childs of child" you can say, I want all attendees of event -JFSDFHdsf89498432`. When you update the question with that, please also show how you invoke `getAttendees` and what value of `child` you're passing in. – Frank van Puffelen Jul 11 '15 at 02:44
  • In my comment (http://stackoverflow.com/questions/31305915/more-efficient-way-to-retrieve-firebase-data#comment50604873_31307494) I used `child_added` for getting the attendees. On iOS `child_added` translates to `FEventTypeChildAdded` and is documented here: https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-event-types – Frank van Puffelen Jul 11 '15 at 02:47
  • If you want to use `.Value`, this question seems to have an example where iterating over the children works: http://stackoverflow.com/questions/27341888/iterate-over-snapshot-children-in-swift-firebase – Frank van Puffelen Jul 11 '15 at 02:52

0 Answers0