我有一个基于 coredata 的 iPhone 应用程序,并且有一个具有纬度和经度作为属性的实体。如何返回按距当前位置的距离排序的所有实体?我以前是在没有 coredata 编写 sql 查询的情况下完成此操作的,但我对 coredata 很陌生,无法弄清楚。我想使用 NSFetchedResultsController 直接传递到我的表格 View 。
请问有什么帮助吗?
最佳答案
根据我的经验,执行此操作的最佳方法是计算首次加载(或位置更改)时每个条目的距离并将该属性保存到 CoreData,这样您就可以使用距离属性轻松获取条目。
这是计算两组坐标之间的距离的代码片段:
for(SWShow *show in shows) {
CLLocation *showLocation = [[CLLocation alloc] initWithLatitude:show.latitude longitude:show.longitude];
double distance = [currentLocation distanceFromLocation:showLocation]/1000 // in km
show.distance = @(distance);
}
[managedObjectContext save];
关于iphone - 如何使用核心数据获取按距离排序的地点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1500512/