Ios 在查询解析表之前获取位置查询
我在获取用户的位置,然后为类运行Ios 在查询解析表之前获取位置查询,ios,xcode,swift,parse-platform,location,Ios,Xcode,Swift,Parse Platform,Location,我在获取用户的位置,然后为类运行PFQuery时遇到问题。我遇到的问题是,当我启动应用程序时,tableview会加载一个空数据集。我知道查询工作正常,因为当我使用下拉方法刷新tableview时,它会加载具有正确位置的正确数据集 override func queryForTable() -> PFQuery! { let query = PFQuery(className: "Posts") if let userLocation = currLocation {
PFQuery
时遇到问题。我遇到的问题是,当我启动应用程序时,tableview会加载一个空数据集。我知道查询工作正常,因为当我使用下拉方法刷新tableview时,它会加载具有正确位置的正确数据集
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
}
因此,我怀疑当我第一次启动应用程序时,查询会在获取位置之前运行,但在我手动刷新tableview之后,位置已从位置管理器中获取,并加载了正确的数据集
我引用了这两篇文章
但是,在将PFGeoPoint.geoPointForCurrentLocationInBackground
放置在viewDidLoad
中时遇到了很多问题,查询在其中,或者将其放置在单独的函数中,查询在其中(我有一个名为queryForData
的单独函数,它允许拉式刷新工作,但我在添加PFGeoPoint.geoPointForCurrentLocationInBackground
时删除了该函数)
无论我做什么,查询都不会成功工作,因为类中的所有行都被加载,而不仅仅是应用于用户当前位置的数据行
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
}
然后我使用了我发布的另一个stackoverflow问题的代码
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
}
}
}
}
谢谢。地理查询完成后,您不会加载表 此外,您不需要在geoQuery块内再次创建查询。您可以调用Parse的
loadObjects
方法,该方法将使用queryForTable()
方法重新加载数据
PFGeoPoint.geoPointForCurrentLocationInBackground {
(point:PFGeoPoint!, error:NSError!) -> Void in
if error == nil {
currentLocation = point
self. loadObjects()
}
}
currentLocation
应该在类型为PFGeoPoint
的控制器中是全局的。你也可以发布你的代码吗?谢谢。嗨,我刚刚用我使用的代码更新了帖子。谢谢!这很有效!谢谢!我还有一个简单的问题。当我在模拟器中运行这个时,它不起作用。但是,当我在实际开发中运行它时冰,它确实有效。你知道是什么导致了这种差异吗?