0

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:

JSON DATABASE

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

ShaneN
  • 21
  • 1
  • i have posted code + explanation. if you have any questions, let me know – David Seek Oct 18 '16 at 23:45
  • i dont know why this was downvoted, it's a good question, perhaps it doesn't reflect the problem for the answer the OP said it solved – aremvee Jun 16 '18 at 03:36

1 Answers1

2

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
            self.peoples.removeAll()

            // 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
                self.tweets.append(t)                    
            }
        }
        // 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() {
    loadPeople()
}

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

for p in peoples {
   print("name = \(p.name)")
   print("longitude = \(p.longitude)")
   print("latitude = \(p.longitude)")
}
David Seek
  • 16,783
  • 19
  • 105
  • 136
  • Hi, thanks for the reply its a great help ! Would it be ok to use a class instead of a struct or is there an advantage in using a struct ? – ShaneN Oct 18 '16 at 23:49
  • According to the very popular WWDC 2015 talk Protocol Oriented Programming in Swift (video, transcript), Swift provides a number of features that make structs better than classes in many circumstances. – David Seek Oct 18 '16 at 23:51
  • http://stackoverflow.com/questions/24232799/why-choose-struct-over-class/24232845 – David Seek Oct 18 '16 at 23:51