Defining a custom sqlite function #1255
aidaan
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a table that includes GPS locations on each row (in columns
latitude
andlongitude
), and I wanted to sort these items by the distance to the users location. I could load all the rows into memory and sort them manually, but I wanted to try to have this sorting accomplished in SQL. The standard build of SQLite does not includePOW
orSQRT
functions, so I can't use them for this calculation. But I was able to create a custom SQLite function to specifically compute this distance, and wanted to pass this along:This creates the
MAP_DISTANCE
function which accepts 4 arguments: the latitude and longitude for the two points to compare. It uses thedistance(to:)
method onMKMapPoint
, because this apparently takes into account the curvature of the earth. With this I can construct a query using this function:I honestly don't know if this is more or less performant than sorting manually. But it's nice to have all of this logic handled in the query.
Beta Was this translation helpful? Give feedback.
All reactions