Thien Thien - 7 months ago 96
Swift Question

List nearby GeoFire locations in tableview

Currently, as my codes below shown, my tableview display everything from Firebase. How do I limit the list to what's nearby?

DataService.dataService.BUSINESS_REF.observeEventType(.Value, withBlock: { snapshot in

// A snapshot of the businesses data

self.businesses = []

if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {

for snap in snapshots {

// Make business array for the tableview
if let postDictionary = snap.value as? Dictionary<String, AnyObject> {

let key = snap.key
let business = Business(key: key, dictionary: postDictionary)

// Show newest business first
self.businesses.insert(business, atIndex: 0)
}
}
}

// Update the table when there is new data

self.searchTableView.reloadData()
})


I'm new to iOS programming and the codes above is from a tutorial, I realise I need to make use of GeoFire's GFQuery objects but I just can't figure out where to put this in my code. Thanks in advance!

Answer

Figured it out.

DataService.dataService.BUSINESS_REF.queryOrderedByKey().queryEqualToValue(key).observeEventType(.Value, withBlock: { snapshot in

    if let snapshots = snapshot.children.allObjects as? [FDataSnapshot]  {

        for snap in snapshots {

            if let postDictionary = snap.value as? Dictionary<String, AnyObject> {

                let business = Business(key: key, dictionary: postDictionary)

                self.businesses.insert(business, atIndex: 0)

            }
        }
    }

    self.searchTableView.reloadData()

})