NSPredicate: distanceToLocation:fromLocation:

This returns nothing even though there are multiple records in the radius. The field name of the location is Location.

There is obviously code after this but that works fine it is just the predicate that doesn't work.


let LocationOfUser = CLLocation(latitude: location.latitude, longitude: location.longitude)

let fixedRadius = NSNumber(integerLiteral: 5000)

let pred = NSPredicate(format: "distanceToLocation:fromLocation:(Location, %@) < %f", argumentArray: [LocationOfUser, fixedRadius])

let sort = NSSortDescriptor(key: "rating", ascending: true)

let query = CKQuery(recordType: RecordType.Items, predicate: pred)

query.sortDescriptors = [sort]

I don't do Swift so I can't help here. But a suggestion - replace your predicate with something easier - like TRUE or some field less than some value to be sure the problem is 'just the predicate'. Then in one of the records returned by that predicate, test your predicate explicitly to be sure it works on those records.

I don't understand what you pass as format: is it a function call ?

https://stackoverflow.com/questions/34529722/nspredicate-with-a-functiondistanceToLocation:fromLocation:(Location, %@)


AFAIK, it is not possible.

Read here as well:

https://stackoverflow.com/questions/34529722/nspredicate-with-a-function


You should probably retrieve more data and filter the results on condition

>I don't understand what you pass as format: is it a function call ?


This works (in ObjectiveC).


        NSDate *thirtyDaysAgo=[NSDate dateWithTimeIntervalSinceNow:-30*24*3600] ;
        //    float radiusInKilometers=5;
        CLLocation *home=[[CLLocation alloc] 
               initWithLatitude:[[[notification object] objectForKey:@"GeoAlat"] doubleValue] 
                      longitude:[[[notification object] objectForKey:@"GeoA"] doubleValue]];
        float radiusInMeters=10000;
        
        NSPredicate *predicate=[NSPredicate 
                  predicateWithFormat:@"distanceToLocation:fromLocation:(HomeLocation,%@) < %f  
                                       AND %@ < TheDateLastAccessed",home,radiusInMeters,thirtyDaysAgo];
        CKQuery *query=[[CKQuery alloc] initWithRecordType:@"Rides" predicate:predicate];
  
NSPredicate: distanceToLocation:fromLocation:
 
 
Q