核心数据中的位置通过 NSFetchedResultsController 按距离排序? [英] Locations in Core Data sorted by distance via NSFetchedResultsController?

查看:19
本文介绍了核心数据中的位置通过 NSFetchedResultsController 按距离排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 iOS 核心数据数据库中有一组实体对象,用于描述某个位置的某些内容.我们将实体称为 Location.我通过在 Location 上有两个引用位置的属性来实现这一点 - 纬度和经度,都是双倍的.还有其他元素,如名称.

I have a set of entity objects in my iOS Core Data database that describe something at a location. Let's call the entity Location. I have implemented this by having two attributes on Location that refer to the location - latitude and longitude, both doubles. There are other elements, like name.

我使用 NSFetchedResultsController 将实体绑定到 UITableViewController.我想要做的是按到给定 CLLocationCoordinate2D 的距离对结果进行排序.在一个非常理想的场景中,我能够刷新该列表以根据新位置重新计算排序.因此,这种排序将取决于两个键和第三个静态"变量(一个不会因集合中的项目而异).

I am using a NSFetchedResultsController to bind the entities to a UITableViewController. What I would like to do is have the results sorted by distance to a given CLLocationCoordinate2D. In an really ideal scenario, I'm able to refresh that list to recalculate the sort based on a new location. Therefore, this sort would depend on two keys, and a third "static" variable (one that doesn't vary across the items in the collection).

如果我使用 NSSortDescriptors 对任意列表进行排序,我想我可以弄清楚如何做到这一点.但是,我无法控制在 NSFetchedResultsController 中如何使用排序描述符.

I think I could figure out how to do this if I was sorting an arbitrary list with NSSortDescriptors. However, I don't control how the sort descriptors are used in an NSFetchedResultsController.

有没有办法可以配置我的实体、我的 NSFetchedResultsController、我的 NSSortDescriptors 等来完成这个?我怀疑答案不在于创建一个花哨的 NSSortDescriptor,而是在实体中创建一个表示与我的距离的瞬态属性,并定期重新计算该属性.但是,我对 Core Data 不够熟悉,我不确定如何最好地做到这一点(遍历所有实体并重新计算字段).我也不确定 NSSortDescriptors 是否适用于瞬态属性.

Is there a way that I could configure my entities, my NSFetchedResultsController, my NSSortDescriptors, etc. to accomplish this? I suspect that the answer lies not in creating a fancy NSSortDescriptor but instead in creating a transient attribute in the entity that represents the distance-to-me, and recalculating that attribute periodically. However, I'm new enough to Core Data that I'm not sure how to best do this (iterate over all entities and recalculate a field). I'm also not sure if NSSortDescriptors will work on Transient attributes.

推荐答案

(来自评论:)

对(基于 SQLite 的)核心数据存储的获取请求不能使用基于瞬态属性或基于 Objective-C 的谓词的排序描述符.

A fetch request for a (SQLite based) Core Data store cannot use sort descriptors based on transient attributes or Objective-C based predicates.

如果您不想失去获取结果控制器的优势(如动画表格视图更新、自动分组到部分等),您必须预先计算到当前位置的距离并将其存储在 (持久性)对象的属性.

If you don't want to loose the advantages of a fetched results controller (like animated table view updates, automatic grouping into sections etc.) you have to pre-compute the distance to the current location and store it in a (persistent) attribute of your objects.

或者,您可以获取所有对象并在内存中对它们进行排序.在这种情况下,您可以使用任意排序描述符.但这不能与获取的结果控制器结合使用,因此您必须在托管对象上下文中注册更改并在必要时重新加载表.

Alternatively, you could fetch all objects and sort them in memory. In that case you can use arbitrary sort descriptors. But that cannot be combined with a fetched results controller, so you would have to register for changes in the managed object context and reload the table if necessary.

这篇关于核心数据中的位置通过 NSFetchedResultsController 按距离排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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