我无法理解为什么我的查询不起作用

时间:2013-09-03 08:51:09

标签: objective-c parse-platform userlocation geopoints pfquery

所以我在parse.com上有一堆对象。类名是“MainInfo”,在名为geoPoint的列中包含地理点。

我通过将以下内容添加到我的.h文件来获取用户位置:

@property (nonatomic, strong) PFGeoPoint *userLocation;

然后将以下内容添加到viewDidLoad:

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
if (!error) {
    self.userLocation = geoPoint;
    [self loadObjects];
}
}];

并执行滚动查询表:

- (PFQuery *)queryForTable
{
// User's location
PFGeoPoint *userGeoPoint = self.userLocation;
// Create a query for places
PFQuery *query = [PFQuery queryWithClassName:@"MainInfo"];
// Interested in locations near user.
[query whereKey:@"geoPoint" nearGeoPoint:userGeoPoint];
// Limit what could be a lot of points.
query.limit = 10;
// Final list of objects
_placesObjects = [query findObjects];

return query;
}

Xcode给出了错误*** setObjectForKey: object cannot be nil (key: $nearSphere)

我不知道我做错了什么,据我所知它应该有用。

我与解析文档一起工作让我走到这一步。 Here is a link

2 个答案:

答案 0 :(得分:2)

当你进行geoPointForCurrentLocationInBackground调用时,它有一个完成块。此完成块标记了您具有填充表视图所需信息的点(或者您知道存在错误,您应该执行其他操作)。因此,在调用完成块之前,不应将查询数据显示/加载到表视图中。另外,您没有完成查询所需的信息。

您可以在等待时显示活动指示器。或者,最好在显示此视图之前获取userLocation,以便在到达此处时始终获得查询的信息。

答案 1 :(得分:1)

出现错误是因为您将nil值传递给whereKey:nearGeoPoint:,因为self.userLocation不太可能在第一次加载视图时设置。你会想要做两件事:

  1. queryForTable方法中,检查self.userLocation是否为零。如果是,则返回nil。这可以作为无操作,表格不会显示任何数据。

    - (PFQuery *)queryForTable
    {
        if (!self.userLocation) {
            return nil;
        }
        // User's location
        PFGeoPoint *userGeoPoint = self.userLocation;
        // Create a query for places
        PFQuery *query = [PFQuery queryWithClassName:@"MainInfo"];
        // Interested in locations near user.
        [query whereKey:@"geoPoint" nearGeoPoint:userGeoPoint];
        // Limit what could be a lot of points.
        query.limit = 10;
        // Final list of objects
        _placesObjects = [query findObjects];
    
        return query;
    }
    
  2. geoPointForCurrentLocationInBackground:完成功能块中,设置self.userLocation值后,您需要拨打[self loadObjects]。这将告诉PFQueryTableViewController再次运行您的查询,这次self.userLocation周围不会为零,允许您构建原始查询。幸运的是,您已经执行了此步骤,但我已将其包含在此处,以防其他人有同样的问题。

相关问题