I was having issues with obtaining the location of the user and then running a PFQuery
for the class. The issue I am having is when I launch the app the tableview loads an empty data set. I know the query is working fine because when I refresh the tableview with the pull down method it loads the right data set with the right location.
So I suspect when I first launch the application the query runs before obtaining the location, but then after I manually refresh the tableview the location has been obtained from the location manager and the right data set is loaded.
I referenced these two posts...
Location Query in Parse TableView
Locations around the users with parse
But, was having a lot of trouble when placing the PFGeoPoint.geoPointForCurrentLocationInBackground
in the viewDidLoad
with the query inside of it or placing it in a separate function with the query inside of it (I have a separate function called queryForData
which allows the pull to refresh to work, but I remove that function when adding the PFGeoPoint.geoPointForCurrentLocationInBackground
)...
Whichever I do, the Query does not work successfully as the all the rows in the class are loaded instead of just the data rows that apply to the user's current location.
override func queryForTable() -> PFQuery! {
let query = PFQuery(className: "Posts")
if let userLocation = currLocation {
query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: userLocation.latitude, longitude: userLocation.longitude), withinKilometers: 5)
query.orderByDescending("createdAt")
} else {
}
return query
}
And then I was using this code from that other stackoverflow question I posted...
PFGeoPoint.geoPointForCurrentLocationInBackground {
(point:PFGeoPoint!, error:NSError!) -> Void in
if error == nil {
//Point contains the user's current point
//Get a max of 100 of the restaurants that are within 5km,
//ordered from nearest to furthest
var query = PFQuery(className: "Posts")
query.limit = 100
query.whereKey("location", nearGeoPoint: point, withinKilometers: 5.0)
query.findObjectsInBackgroundWithBlock{
(objects: [AnyObject]!, error: NSError!) -> Void in
if (error == nil) {
//objects contains the restaurants
}
}
}
}
Thank you.