ios - More efficient way to retrieve Firebase Data? -
i have hierarchical set of data want retrieve information firebase. below how data looks:
however, issue this: upon looking @ how data structured, when want grab name
or object id
of attendee
, have perform following code:
func getattendees(child: nsstring, completion: (result: bool, name: string?, objectid: string?) -> void){ var attendeesreference = self.eventsreference.childbyappendingpath((child string) + "/attendees") attendeesreference.observeeventtype(feventtype.childadded, withblock: { (snapshot) -> void in //get name/object id of attendee 1 one--inefficient? let name = snapshot.value.objectforkey("name") as? string let objectid = snapshot.value.objectforkey("objectid") as? string if snapshot != nil { println("name: \(name) object id: \(objectid)") completion(result: true, name: name, objectid: objectid) } }) { (error) -> void in println(error.description) } }
now goes through every attendee child , grabs name , object id 1 one. when function completes, store each value dictionary. upon doing so, function called multiple times , can slow when going to/from database many times. there more efficient way this? have tried feeventtype.value
seems return within attendees
child, when want name
, objectid
of each attendee
stored sort of dictionary. appreciated. thanks!
one of golden rules of firebase only nest data when want retrieve of it. reason rule firebase returns complete node. cannot partially retrieve of data in node , not other data.
the firebase guide on structuring data, says it:
because can nest data 32 levels deep, it's tempting think should default structure. however, when fetch data @ location in our database, retrieve of child nodes. therefore, in practice, it's best keep things flat possible, 1 structure sql tables.
you should read entire section of docs, since contains pretty examples.
in case, you'll need modify data structure separate event attendees event metadata:
events -jfsdfhdsf89498432 eventcreator: "stephen" eventcreatorid: 1764137 -joedfjhfdshj14312 eventcreator: "puf" eventcreatorid: 892312 event_attendees -jfsdfhdsf89498432 -jsajkas75478 name: "johnny appleseed" -jsajkas75412 name: "use1871869" -joedfjhfdshj14312 -jaaasdhj1382 name: "frank van puffelen" -jo1asd138921 name: "use1871869"
this way, can retrieve event metadata without retrieving attendees , vice versa.
Comments
Post a Comment