Objective c 我能';我不明白为什么我的查询不起作用
所以我在parse.com上有一堆对象。类名为“mainfo”,在名为geoPoint的列中包含地理点 我通过将以下内容添加到我的.h文件来获取用户位置:Objective c 我能';我不明白为什么我的查询不起作用,objective-c,parse-platform,userlocation,geopoints,pfquery,Objective C,Parse Platform,Userlocation,Geopoints,Pfquery,所以我在parse.com上有一堆对象。类名为“mainfo”,在名为geoPoint的列中包含地理点 我通过将以下内容添加到我的.h文件来获取用户位置: @property (nonatomic, strong) PFGeoPoint *userLocation; 然后将以下内容添加到viewDidLoad: [PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
@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不能为零(key:$nearSphere)
我不知道我做错了什么,就我所能看到的而言,这应该是可行的
我与解析文档一起工作,使我走到了这一步 当您为CurrentLocationInBackground调用
地理点时,它有一个完成块。此完成块标记您具有填充表视图所需信息的点(或者您知道有错误,应该执行其他操作)。因此,在调用完成块之前,不应该将查询数据显示/加载到表视图中。另一方面,您没有完成查询所需的信息
您可以在等待时显示活动指示器。或者,最好在显示此视图之前获取userLocation
,以便在到达此处时始终具有查询信息。出现错误是因为您将nil值传递给其中key:nearGeoPoint:
作为self。在首次加载视图时不太可能设置userLocation
。您需要做两件事:
在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;
}
在geoPointForCurrentLocationInBackground:
完成块中,一旦设置了self.userLocation
值,您将需要调用[self-loadObjects]
。这将告诉PFQueryTableViewController
再次运行您的查询,这一次self.userLocation
将不会为零,允许您构建原始查询。幸运的是,您已经执行了这个步骤,但我已经将它包括在这里,以防其他人有相同的问题
self.userLocation
当时是否已更新?@Wain否尚未更新,如何使查询等待到找到用户位置?如何使tableview等待到获得位置后再加载数据?您可以从queryForTable
返回nil
,然后在创建真正的查询后从完成块调用reloadData
。不确定这是否能完美工作-取决于表视图处理nil
。