I am using Swift, CoreLocation, and Parse in my app. I have a couple questions that I hope someone can answer please:
1) When exactly does the queryForTable method get called?
2) Why is the user's current location (CLLocationCoordinate2D) not available from the get go when the app loads (currLocation
seems to be nil at first here: if let queryLoc = currLocation
)?
See comments in part of my code below.
var currLocation: PFGeoPoint?
override func viewDidLoad() {
super.viewDidLoad()
queryForPosts()
}
func queryForPosts() {
PFGeoPoint.geoPointForCurrentLocationInBackground {
(geoPoint: PFGeoPoint?, error: NSError?) -> Void in
if error == nil {
// do something with the new geoPoint
self.currLocation = geoPoint
// this will call queryForTable
self.loadObjects()
}
}
}
override func queryForTable() -> PFQuery {
let query = PFQuery(className: ParseHelper.ParsePostClass)
// Need to fix: the query is being run before the location manager returns a valid location (currLocation)...
if let queryLoc = self.currLocation {
print("SUCESS: Successfully queried user's location.")
query.whereKey("location", nearGeoPoint: queryLoc, withinMiles: 3)
query.limit = 200;
if (self.objects?.count == 0) {
query.cachePolicy = PFCachePolicy.CacheThenNetwork
}
query.addDescendingOrder("createdAt")
return query
} else {
// This else block is being executed first every time the view loads. Only after the user performs a pull to refresh on the table view, the if block gets executed and the correct objects display.
print("FAILURE: Could not query user's location.")
/* Decide on how the application should react if there is no location available */
}
return PFQuery()
}