ios - withinMiles 无法在 Swift 中使用 Parse PFGeopoint 查询

标签 ios swift xcode parse-platform coordinates

我试图找到当前用户的 PFGeopoint 一定距离内的所有用户。使用时查询返回对象:

query!.whereKey("addressPoint", nearGeoPoint: userGeopoint)

但它在使用时不返回任何对象:

query!.whereKey("addressPoint", nearGeoPoint: userGeopoint, withinMiles: 100)

库比蒂诺模拟器位置 100 英里范围内有近 20 个用户,所以这不是问题。

完整代码如下:

覆盖函数 viewDidLoad() {

    super.viewDidLoad()

var userGeopoint = PFGeoPoint()

    PFGeoPoint.geoPointForCurrentLocationInBackground {
        (geoPoint: PFGeoPoint?, error: NSError?) -> Void in

        if error == nil {
            userGeopoint = geoPoint!
            //print(userGeopoint)
        }
    }

    let query = PFUser.query()
    query!.whereKey("addressPoint", nearGeoPoint: userGeopoint, withinMiles: 1000)
    query!.limit = 20
    query!.orderByDescending("updatedAt")
    query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

        if error == nil {

        if let users = objects {
        for object in users {
                if let user = object as? PFUser {
                    if user != PFUser.currentUser() {
                        print(user["addressPoint"])
                        }
                    }
            }

} } 别的 { 打印(错误) } })

最佳答案

获取用户位置可能需要几秒钟,这就是为什么 PFGeoPoint.geoPointForCurrentLocationInBackground 没有在主线程上运行的原因,将您的查询放在其中,它会起作用...另外我认为按距离限制的查询仅限于 100 英里

PFGeoPoint.geoPointForCurrentLocationInBackground {
    (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
    if error == nil {
        let query = PFUser.query()
        query!.whereKey("addressPoint", nearGeoPoint: geoPoint, withinMiles: 100)
        //... rest of the query
    }
}

关于ios - withinMiles 无法在 Swift 中使用 Parse PFGeopoint 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38649351/

相关文章:

iphone - 从 UITableView 中删除单元格 - 错误

ios - Google Cast 设备 DidComeOnline(设备 : GCKDevice!)从未被调用 #GoogleCastSDK

ios - swift - 如何使用 NSURL 数组添加到收藏夹

iphone - 用iPhone App替换Sqlite中单行中的单列值

iphone - iOS 8 颠倒方向,启用 XCode 选项,仍然不起作用

IOS/Objective-C : App crashes with core data - pressing the save button

ios - 为什么 Cordova 插件的 native View 是堆叠的,我如何禁用它们

ios - Swift 错误 - 使用未声明的类型 'cell' - Collection View

sorting - 基于其他排序数组对数组进行排序

xcode - 如何将 LLDB 的默认语言设置为 Swift?