1

I try to run a compound query that returns objects near a GeoPint. I only want to retrieve objects that are junger than 24h. Somehow my code returns 0. objects.. I only found that OrQueries are not possible with GeoPoints. I would expect AND Queries to work. 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]);
    }
}];
Jan Wilhelm
  • 111
  • 7
  • After you create the date please show what happens with `NSLog(@"$@", queryDate);` I don't think that's doing what you think it's doing. – Fogmeister May 27 '14 at 12:51

1 Answers1

0

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.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Hi, what do you think is wrong with the date? I think you only have a shorter form of expressing the date, right? Or do I overlook something? I tested the query with your date and still no result... – Jan Wilhelm May 27 '14 at 13:33
  • Did you do what I suggested in my comment? – Fogmeister May 27 '14 at 14:04
  • @JanWilhelm Also, they do create different dates. First is today's date, second uses your method, third uses my method. `2014-05-27 14:09:57 +0000` -- `2014-05-25 23:00:00 +0000` -- `2014-05-26 14:09:57 +0000` are you certain there is data that should be returned? i.e. are you sure it isn't valid in returning no data? – Fogmeister May 27 '14 at 14:11
  • I agree that there is an issue in my date creation. But that does not resolve my problem. I tested a Geo Query with 2 constraints like: [query whereKey:@"location" nearGeoPoint:point];[query whereKey:@"topic" equalTo:@"Something"]; >> Result is still an empty array. – Jan Wilhelm Jun 02 '14 at 08:53
  • @JanWilhelm Hmm... have you checked that data actually exists for the query you are running? The only other thing I can think is to remove each constraint in turn. i.e. constrain on just geo point to check that comes through and then constrain just on date to check that comes through also. Just to triple check the data being returned is what you expected. – Fogmeister Jun 02 '14 at 09:20
  • I tested all versions. Still no success. Did you ever manage to run a Geo query with and additional constraint? – Jan Wilhelm Jun 03 '14 at 14:47
  • @JanWilhelm What do you mean all versions? And no success? Do you mean you removed the Geo constraint and it still failed? If that's the case then it's nothing to do with the Geo constraint. – Fogmeister Jun 03 '14 at 14:49
  • I tested several different combinations. I eliminating the risk that the date is set in a wrong way by setting a constraint of a string value (That exists in my data). Still the query returns 0 objects.. – Jan Wilhelm Jun 05 '14 at 11:47
  • Then there is something wrong somewhere else in your project. Not in the creation of this query. Have you checked everything. Like making sure the PFObject class names are correct and the Parse access token is correct, etc... Also, check the read access on the objects you are querying. Do you have read access on them. etc... – Fogmeister Jun 05 '14 at 11:56
  • Yes - I checked everything (That I could think off). Did you ever manage to run a query with a geo query and any other constraint? – Jan Wilhelm Jun 15 '14 at 15:48
  • I'll try to add a constraint to my gripping query and let you know. – Fogmeister Jun 15 '14 at 17:40