问题描述
我在iOS Core Data数据库中有一组实体对象,用于描述某个位置的内容。让我们调用实体Location。我已经实现了这个通过在位置有两个属性,指的位置 - 纬度和经度,两个双。还有其他元素,如name。
我使用NSFetchedResultsController将实体绑定到一个UITableViewController。我想做的是将结果按距离排序到给定的CLLocationCoordinate2D。在一个非常理想的情况下,我可以刷新该列表,以便根据新位置重新计算排序。因此,这种类型将取决于两个键和第三个静态变量(在集合中的项目之间不变)。
我认为我可以找出如何做到这一点,如果我使用NSSortDescriptors排序任意列表。但是,我不控制如何在NSFetchedResultsController中使用排序描述符。
有一种方法,我可以配置我的实体,我的NSFetchedResultsController,我的NSSortDescriptors等。来完成这个?我怀疑答案不在于创建一个花哨的NSSortDescriptor,而是在实体中创建一个临时属性,表示距离到我,并定期重新计算该属性。但是,我对Core Data很新,我不知道如何做到这一点(迭代所有实体并重新计算字段)。我也不确定NSSortDescriptors是否将工作在瞬态属性。
(来自评论:)
针对(基于SQLite的)Core Data存储的提取请求不能使用基于临时属性或基于Objective-C的谓词的排序描述符。
如果您不想要松散获取结果控制器的优点(如动画表视图更新,自动分组成节等),你必须预先计算到当前位置的距离,并将其存储在对象的(持久)属性。 / p>
或者,您可以获取所有对象并在内存中对它们进行排序。在这种情况下,您可以使用任意排序描述符。但是不能与获取的结果控制器结合使用,因此您必须在托管对象上下文中注册更改,并在必要时重新加载表。
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.
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).
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.
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.
(From the comments:)
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排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!