Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More efficient way to retrieve Firebase Data?

I have a hierarchical set of data that I want to retrieve information from Firebase. Below is how my data looks:enter image description here

However, my issue is this: Upon looking at how the data is structured, when I want to grab the name or object id of an attendee, I have to perform the following code:

func getAttendees(child: NSString, completion: (result: Bool, name: String?, objectID: String?) -> Void){
    var attendeesReference = self.eventsReference.childByAppendingPath((child as String) + "/attendees")

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

        //Get the name/object ID of the attendee one by 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 this goes through every attendee child and grabs the name and object id one by one. When the function completes, I store each value into a dictionary. Upon doing so, this function is called multiple times and can be very slow especially when going to/from a database so many times. Is there a more efficient way to do this? I have tried to look into FEeventType.Value but that seems to return everything within the attendees child, when all I really want are the name and objectID of each attendee stored into some sort of dictionary. Any help would be appreciated. Thanks!

like image 654
user1871869 Avatar asked Dec 15 '22 13:12

user1871869


1 Answers

One of the golden rules of Firebase is to only nest your data when you always want to retrieve all of it. The reason for this rule is that Firebase always returns a complete node. You cannot partially retrieve some of the data in that node and not other data.

The Firebase guide on structuring data, says this about it:

Because we can nest data up to 32 levels deep, it's tempting to think that this should be the default structure. However, when we fetch data at a location in our database, we also retrieve all of its child nodes. Therefore, in practice, it's best to keep things as flat as possible, just as one would structure SQL tables.

You should really read that entire section of the docs, since it contains some pretty good examples.

In your case, you'll need to modify your data structure to separate the event attendees from the 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, you can retrieve the event metadata without retrieving the attendees and vice versa.

like image 72
Frank van Puffelen Avatar answered Dec 27 '22 17:12

Frank van Puffelen