通过 NSFetchedResultsController 按距离排序的核心数据

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

问题描述

我的 iOS Core Data 数据库中有一组实体对象,用于描述某个位置的某些内容.让我们称实体位置.我通过在 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)Core Data 存储的获取请求不能使用基于瞬态属性或基于 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 按距离排序的核心数据中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)