I am using Parse version "1.14.4" iOS 10.3.2 and swift 3. The query is slow whether local (he objects returned are pinned) or remote. Thanks
let placeObject = PFObject(className:"PlaceObject")
let point = PFGeoPoint(latitude:self.PointGlobal.latitude, longitude:self.PointGlobal.longitude)
placeObject["location"] = point
let query = PFQuery(className:"CLocationObject")
// Interested in locations near user.
query.whereKey("location", nearGeoPoint:point)
// Limit what could be a lot of points.
query.limit = 200
let localQuery = (query.copy() as! PFQuery).fromLocalDatastore()
localQuery.findObjectsInBackground{
(objects: [PFObject]?, error: Error?) -> Void in
self.dataReturnedLocally = true
.....
if self.dataReturnedLocally{
print("local query with no error there was data already")
}
else {
print("getting data remotely")
query.findObjectsInBackground{
(objects: [PFObject]?, error: Error?) -> Void in
if error == nil {
if let objects = objects {
geo based queries are the slowest types of queries with MongoDB, unfortunately. Also, there are not automatically indexes based on location, making these extra slow, especially for large collections. So, your only real solution is to add indexes to your database to index the location, optimized for the location queries you'll need to make. Though keep in mind too many of these affects write speed.
Depending on your use case, it may be better to use
withinMiles
instead ofnearGeoPoint
. This will return fewer results, but will not take as long to run, either.