CloudKit predicate

Can someone help me identify what is wrong with my predicate string, please?

query = CKQuery(recordType: RecordType, predicate: NSPredicate(format: "(Lat > "+String(currentPlaceLat - limit)+") AND (Lat < "+String(currentPlaceLat + limit)+") AND (Long > "+String(currentPlaceLong - limit)+") AND (Long < "+String(currentPlaceLong + limit)+")")

This is what it prints during runtime:

(Lat > 39.872) AND (Lat < 49.872) AND (Long > -85.493) AND (Long < -75.493)

I want to filter out the records that are between that latitude and that longitude, but Im sure there is something wrong with my query.

Sorry, I am new to CloudKit.

Accepted Reply

Solved with this:

let predicateString = "Lat BETWEEN {" + String(currentPlaceLat - limit) + ", " + String(currentPlaceLat + limit) + "}" + " && " + "Long BETWEEN {" + String(currentPlaceLong - limit) + ", " + String(currentPlaceLong + limit) + "}"

Replies

CloudKit has native support for location fields. See https://developer.apple.com/library/archive/documentation/DataManagement/Conceptual/CloudKitQuickStart/AddingAssetsandLocations/AddingAssetsandLocations.html for a query example.

Yes, I know, but I’m avoiding them because I need more precision and I understand these have a 10km precision. Thanks. I want to implement in the way I described.

Solved with this:

let predicateString = "Lat BETWEEN {" + String(currentPlaceLat - limit) + ", " + String(currentPlaceLat + limit) + "}" + " && " + "Long BETWEEN {" + String(currentPlaceLong - limit) + ", " + String(currentPlaceLong + limit) + "}"