【发布时间】:2015-07-05 07:13:34
【问题描述】:
我在获取用户位置然后为班级运行PFQuery 时遇到问题。我遇到的问题是当我启动应用程序时,tableview 会加载一个空数据集。我知道查询工作正常,因为当我使用下拉方法刷新 tableview 时,它会在正确的位置加载正确的数据集。
所以我怀疑当我第一次启动应用程序时,查询会在获取位置之前运行,但是在我手动刷新 tableview 后,位置已经从位置管理器中获取并加载了正确的数据集。
我参考了这两个帖子...
Location Query in Parse TableView
Locations around the users with parse
但是,将 PFGeoPoint.geoPointForCurrentLocationInBackground 放在 viewDidLoad 中并在其中放置查询或将其放置在带有查询的单独函数中时遇到了很多麻烦(我有一个名为 @987654326 的单独函数@ 允许拉动刷新工作,但我在添加 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
}
}
}
}
谢谢。
【问题讨论】:
-
您也可以发布您的代码吗?谢谢。
-
嗨,我刚刚用我正在使用的代码更新了帖子。谢谢!
标签: ios xcode swift parse-platform location