在距离上对许多位置进行排序的最快方法是什么? [英] What is the fastest way to sort a lot of locations on distance?

查看:116
本文介绍了在距离上对许多位置进行排序的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要对许多位置(航路点)到当前位置的距离进行排序。
当然,当前位置是一个移动目标,因此对于每个位置更新,都需要重新计算每个位置的距离。

I want to sort lots of locations (waypoints) on their distance from the current location. The current location is, of course, a moving target, so for every location update, recomputing the distance for every location is necessary. But only recomputing for close-by locations would by enough.

我目前使用核心数据,并将到当前位置的距离作为一个属性存储在表中(但仅在更改时才进行更新),方法是在configurecell:atindexpath:方法中。
这种工作方式,但是当核心数据自动更新所有距离时,应用程序没有响应。这适用于250个位置,但有5000个会崩溃。
我需要它在10.000个位置工作,尽管我可能只需要1000个附近的位置。

I currently use core-data, and store the distance to the current location as an attribute in the table (but only update is when it is changed) from within the configurecell: atindexpath: method. That sort of works, but the application is not responding while core-data automagically is updating all distances. This works for 250 locations, but for 5000 it crashes. I need it to work for 10.000 locations, although I probably only need the 1000 closest locations or so.

我还没有尝试过的想法:
将所有距离存储在单独的内存数组中,仅记录id和距离。然后按距离对数组进行排序。问题是我不能使用FetchedResultsController,因为数据库中没有排序字段。

Ideas that I did not try yet: Store all distances in a separate in-memory array with nothing but record id and distance. Then sort the array on distance. Problem is that then I can not use a FetchedResultsController because there is no sort field in the database.

使用谓词根据纬度和经度过滤位置。然后只显示已过滤的位置。

Filter locations based on their latitude and longitude by using a predicate. Then only present the filtered locations.

在单独的线程中重新计算距离。

Do the recalculation of distances in a separate thread.

有人提出建议,有不同的想法,对我的想法有所变化吗?

Anybody with suggestions, different ideas, a variation on my ideas?

推荐答案

我的最终解决方案如下:
我选择了经度和纬度为1度以内的所有航路点(通常约为1000个航路点),然后计算并存储与当前距离在表格中的位置。然后,我可以对距离进行排序。最慢的是保存核心数据。但是在排序(并提取)之后,我只是取消了更改。节省的费用占整个事情的90%以上,因此效果很好。

My final solution is as follows: I select all waypoints within 1 degree latitude and longitude (about 1000 waypoints normally) and then compute and store the distance to the current position in the table. I can then sort on the distance. The slow thing would be the saving in Core Data. But after sorting (and fetching), I simply cancel the changes. The saving was taking more than 90 % of the whole thing, so this worked quite well.

这篇关于在距离上对许多位置进行排序的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆