过去4天一直在尝试使用此代码来尝试调用PFUser位置,然后将其拉出"位置"。从收到"位置"我希望根据用户位置按照升序对照片数组进行排序。但是,用户位置未正确填充,并且最终为PFUser位置为零。
(NSArray *)caches {
PFGeoPoint *userGeoPoint = [PFUser currentUser][@"location"];
PFQuery *query = [Cache query];
[query whereKey:@"location" nearGeoPoint:userGeoPoint withinMiles:20];
query.limit = 20;
NSMutableArray *photoArray = [[query findObjects] mutableCopy];
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint,
NSError *error){
if (!error) {
[[PFUser currentUser] setObject:geoPoint forKey:@"currentLocation"];
[[PFUser currentUser] saveInBackground];
}
}];
return photoArray;
}
答案 0 :(得分:0)
您尝试这样做的方式存在一些问题:
我建议这样做。启动应用程序后调用此函数以在后台更新用户的位置:
- (void)updateUserLocation {
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
if (!error) {
[[PFUser currentUser] setObject:geoPoint forKey:@"location"];
[[PFUser currentUser] saveInBackground];
}
}];
}
然后,当您需要获取照片时,请调用此功能以在后台获取照片:
- (void)getPhotosInBackground:(void (^)(NSArray *photos, NSError *error))block {
PFGeoPoint *userGeoPoint = [PFUser currentUser][@"location"];
PFQuery *query = [Cache query];
[query whereKey:@"location" nearGeoPoint:userGeoPoint withinMiles:20];
[query setLimit:20];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
block(objects, error);
}];
}