1

我需要使用我的应用获取最近的 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");
     }
 }];

现在我想通过 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;
}

不幸的是,它不起作用。我的数组似乎没有条目。有人可以为我澄清一下,如何以正确的方式使用查询?

干杯,大卫

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

4

1 回答 1

5

首先快速评论

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

你最好使用代码...

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

这与findObjects查询相同。你应该用...

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

实际答案

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

您是否在应用程序委托中设置默认读取访问权限,如下所示...

PFACL *defaultACL = [PFACL ACL];
[defaultACL setPublicReadAccess:YES];
[PFACL setDefaultACL:defaultACL withAccessForCurrentUser:YES];

另外,也许尝试放松约束。1 公里是一个非常小的要检查的半径。

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

应该是@"_User"

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

PFQuery *userQuery = [PFUser query];

当您PFObject正确地对类进行子类化时,它具有此方法,该方法将为您生成正确的查询。

于 2014-06-23T17:06:23.547 回答