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.