Parse.com让附近的用户

deh*_*len 1 cllocationmanager ios parse-platform pfquery

我需要使用我的应用程序获取15个最近用户的列表.当前用户的当前位置存储如下:

PFGeoPoint *currentLocation =  [PFGeoPoint geoPointWithLocation:newLocation];
PFUser *currentUser = [PFUser currentUser];
[currentUser setObject:currentLocation forKey:@"location"];
[currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
     if (!error)
     {
         NSLog(@"Saved Users Location");
     }
 }];
Run Code Online (Sandbox Code Playgroud)

现在我想通过PFQuery检索附近的用户,如下所示:

- (NSArray *)findUsersNearby:(CLLocation *)location
{

PFGeoPoint *currentLocation =  [PFGeoPoint geoPointWithLocation:location];
PFQuery *locationQuery = [PFQuery queryWithClassName:@"User"];

[locationQuery whereKey:@"location" nearGeoPoint:currentLocation withinKilometers:1.0];
locationQuery.limit = 15;
NSArray *nearbyUsers = [locationQuery findObjects];
return nearbyUsers;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是它不起作用.我的阵列似乎没有条目.有人可以为我清理一下,如何正确使用查询?

干杯,大卫

(也发布于:https://www.parse.com/questions/pfquery-to-retrieve-users-nearby)

Fog*_*ter 5

首先快速评论

创建地理点的代码是一个"长时间运行的进程",当您在主线程上运行它时,您可能会看到它出现在控制台中.这意味着应用程序被阻止(冻结),直到返回地理位置.

你最好使用代码......

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
    // Now use the geopoint
}];
Run Code Online (Sandbox Code Playgroud)

对于findObjects查询,这是相同的.你应该用...

[locationQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    // use the objects
}];
Run Code Online (Sandbox Code Playgroud)

实际答案

我想这是一个读访问问题.当您访问User表时,默认情况下,该表没有公共读取权限.

您是否在app app委托中设置了默认的读访问权限...

PFACL *defaultACL = [PFACL ACL];
[defaultACL setPublicReadAccess:YES];
[PFACL setDefaultACL:defaultACL withAccessForCurrentUser:YES];
Run Code Online (Sandbox Code Playgroud)

此外,也许可以尝试放松约束.1km是一个非常小的半径要检查.

啊,我刚发现的其他东西.[PFQuery queryWithClassName:@"User"];使用错误的类名.

它应该是@"_User".

但是,更好的解决方案是使用该类生成查询...

PFQuery *userQuery = [PFUser query];
Run Code Online (Sandbox Code Playgroud)

PFObject正确地为类创建子类时,它具有此方法,可以为您生成正确的查询.