我在获取用户位置然后为类运行 PFQuery
时遇到问题。我遇到的问题是当我启动应用程序时,表格 View 加载一个空数据集。我知道查询工作正常,因为当我使用下拉方法刷新表格 View 时,它会在正确的位置加载正确的数据集。
因此,我怀疑当我第一次启动应用程序时,查询会在获取位置之前运行,但在我手动刷新表格 View 后,已从位置管理器获取了位置并加载了正确的数据集。
我引用了这两篇文章...
Location Query in Parse TableView
Locations around the users with parse
但是,在将 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 完成后,您不会加载表格。
此外,您不需要在 geoQuery block 内再次创建查询。您可以调用 Parse 的 loadObjects
方法,该方法将使用 queryForTable()
方法重新加载数据。
PFGeoPoint.geoPointForCurrentLocationInBackground {
(point:PFGeoPoint!, error:NSError!) -> Void in
if error == nil {
currentLocation = point
self. loadObjects()
}
}
currentLocation
应该是 PFGeoPoint
类型的 Controller 中的全局
关于ios - 在查询解析表之前获取位置查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31227997/