使用GeoPoint Parse.com iOS进行复合查询中的AND运算符

时间:2014-05-27 11:59:37

标签: ios parse-platform mbaas

我尝试运行一个返回GeoPint附近对象的复​​合查询。我只想检索超过24小时的对象。不知何故,我的代码返回0.对象..我只发现GeoPoints无法使用OrQueries。我希望AND Queries能够工作。感谢您提供有关如何解决该问题的任何提示或建议。

NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:-(24)];

NSDate *queryDate = [calendar dateFromComponents:components];

PFGeoPoint *point = [PFGeoPoint geoPointWithLatitude:self.currentLocation.coordinate.latitude longitude:self.currentLocation.coordinate.longitude];

PFQuery *query = [PFQuery queryWithClassName:@"GeoTag"];
[query whereKey:@"createdAt" greaterThanOrEqualTo:queryDate];
[query whereKey:@"location" nearGeoPoint:point];

// Limit what could be a lot of points.
query.limit = 10;

// Final list of objects
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.
        NSLog(@"Successfully retrieved %ld objects.", objects.count);
        // Do something with the found objects
        for (PFObject *object in objects) {
            NSLog(@"%@", object.objectId);
        }
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

1 个答案:

答案 0 :(得分:0)

啊,好吧,你正在使用错误的方法创建日期。

你应该这样做......

NSDateComponents *components = [NSDateComponents new];
[components setHour:-24];

NSDate *queryDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:[NSDate date] options:0];

然后运行您的查询。