【问题标题】:I can't understand why my query is not working我不明白为什么我的查询不起作用
【发布时间】:2013-09-03 08:51:08
【问题描述】:

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

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

@property (nonatomic, strong) PFGeoPoint *userLocation;

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

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

并执行滚动 queryForTable:

- (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

【问题讨论】:

  • self.userLocation当时更新了吗?
  • @Wain 不,它还没有更新,我怎样才能让查询等到找到用户位置?

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


【解决方案1】:

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

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

【讨论】:

  • 如何让 tableview 等到我获得位置后再加载数据?
  • 您可以从queryForTable 返回nil,然后在可以创建真正的查询后从完成块中调用reloadData。不确定这是否会完美运行 - 取决于表视图处理 nil
【解决方案2】:

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

  1. 在您的 queryForTable 方法中,检查 self.userLocation 是否为 nil。如果是,则返回 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 将不会为零,允许您构建原始查询。幸运的是,您已经执行了此步骤,但我将其包含在此处以防其他人有同样的问题。

【讨论】:

    猜你喜欢
    • 2019-07-09
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2021-07-10
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多