ShaneN ShaneN - 1 year ago 93
Swift Question

How to access a certain group of key values in Firebase Database with Swift?

Im new to using Firebase and am struggling to understand how to reach certain points of my firebase database using an ios query with swift.

My database looks something like this:


Im trying to retrieve all the data and then target the location data to put pins on a mapview.
I have the Firebase and FirebaseDatabase pods built into my app with no problem but don't really know where to go from there.
Any help would be greatly appreciated

Answer Source

What I would do is the following:

First I would create a struct for People, to save a model for every Person entry.

You create a new Swift file and enter the following:

struct People {

    var name: String = ""
    var age: String = ""
    var latitude: String = ""
    var longitude: String = ""
    var nationality: String = ""

Then, in your ViewController class, you create an NSArray of People and instantiate is as empty.

var peoples: [People] = []

Then you create a function to download your desired data.

func loadPeople() {

    // first you need to get into your desired .child, what is in your case People
    let usersRef = firebase.child("People")
    usersRef.observeEventType(.Value, withBlock: { snapshot in

        if snapshot.exists() {

            // since we're using an observer, to handle the case
            // that during runtime people might get appended to 
            // the firebase, we need to removeAll, so we don't
            // store people multiple times

            // then we sort our array by Name
            let sorted = (snapshot.value!.allValues as NSArray).sortedArrayUsingDescriptors([NSSortDescriptor(key: "Name",ascending: false)])

            for element in sorted {

                let name = element.valueForKey("Name")! as? String
                let age = element.valueForKey("age")! as? String
                let location = element.valueForKey("location")! as? NSDictionary
                let nationality = element.valueForKey("nationality")! as? String

                // then we need to get our lat/long out of our location dict
                let latitude = location.valueForKey("latitude")! as? String
                let longitude = location.valueForKey("longitude")! as? String

                // then we create a model of People
                let p = People(name: name!, age: age!, latitude: latitude!, longitude: longitude!, nationality: nationality!)

                // then we append it to our Array
        // if we want to populate a table view, we reload it here
        // self.tableView.reloadData()

We need to call the function in viewDidAppear, after the UITableView for example, is loaded.

override viewDidAppear() {

Now we have an Array of People and are able to populate a UITableView or print the values:

for p in peoples {
   print("name = \(")
   print("longitude = \(p.longitude)")
   print("latitude = \(p.longitude)")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download