简体   繁体   English

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

[英]AND operator in compound queries with GeoPoint Parse.com iOS

I try to run a compound query that returns objects near a GeoPint. 我尝试运行一个复合查询,该查询返回GeoPint附近的对象。 I only want to retrieve objects that are junger than 24h. 我只想检索比24h更乱的对象。 Somehow my code returns 0. objects.. I only found that OrQueries are not possible with GeoPoints. 我的代码以某种方式返回0.对象。.我仅发现使用GeoPoints无法实现OrQueries。 I would expect AND Queries to work. 我希望AND查询能够正常工作。 Thanks for any hints or advices how to solve that issue. 感谢您对如何解决该问题的任何提示或建议。

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]);
    }
}];

Ah, ok, you're using the wrong method to create the date. 好的,您使用的是错误的方法来创建日期。

You should do something like this... 你应该做这样的事情...

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

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

Then run your query. 然后运行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM