按用户位置对 NSFetchedResultsController 结果进行排序

发布于 2024-08-29 09:57:14 字数 440 浏览 7 评论 0原文

我有一个应用程序,其中包含核心数据中的一些位置,我想按照与用户位置的接近程度的顺序向用户显示它们。我正在使用 NSFetchedResultsController 为表视图提供位置。我考虑过创建一个虚拟访问器方法,该方法返回距用户的位置距离,该距离将使用“全局”CoreLocationManager 进行计算,但它因原因而崩溃:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“在实体中找不到 keypath distanceFromCurrentLocation

” NSSQLEntity 位置 id=4>'

我还为用户提供了按字母顺序排序的选项,因此如果可能的话,如果我保留 NSFetchedResultsController,我会更喜欢它。

我该怎么做呢?

谢谢!

I have an application that contains some Locations in Core Data, and I want to show them to the user in order of proximity to the user's location. I am using an NSFetchedResultsController to serve the locations to the Table View. I thought about creating a virtual accessor method that returns the location's distance from the user, that would be calculated using a "global" CoreLocationManager, but it crashes with reason:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath distanceFromCurrentLocation not found in entity < NSSQLEntity Location id=4>'

I also give the user the option to sort alphabetically, so I would prefer it if I kept the NSFetchedResultsController, if possible.

How should I do it?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沙沙粒小 2024-09-05 09:57:15

NSFetchedResultsController 的问题(或缺少的功能)是缺少使用瞬态属性进行排序顺序和部分键路径的功能。

我也为此苦苦挣扎,所以你可能对此感兴趣:

NSFetchedResultsController 部分本地化排序< /a>

如果您可以保证有限的记录集,我相信您也可以使用这种方法。但我真的很想看到一个更复杂的解决方案。

The problem (or missing feature) of NSFetchedResultsController is the missing capability to use transient attributes for sort order and section key path.

I struggled with this as well, so you might be interested in this:

NSFetchedResultsController sections localized sorted

I'm sure you could use this approach as well if you could guarantee a limited set of records. But I really would love to see a more sophisticated solution.

洛阳烟雨空心柳 2024-09-05 09:57:15

除非您在实体中定义了瞬态属性,否则虚拟访问器将无法在 Core Data 中工作。 NSFetchedResultsController(以及所有其他核心数据操作)在尝试执行任何操作之前查看实体图。如果虚拟属性没有出现在实体定义中,那么 NSFetchedResultsController 将报告不存在这样的属性。

您想要在名为 distanceFromCurrentLocation 的实体中定义一个瞬态属性,并且您应该能够编写虚拟访问器并让 NSFetchedResultsController 看到它。

Virtual accessors won't work in Core Data unless you have a transient attribute defined in the entity. NSFetchedResultsController (and all other Core Data operations) look at the graph of entities before they try to perform any operation. If the virtual property does not show up in the entity definition, then NSFetchedResultsController will report that no such attribute exist.

You want to define a transient attribute in the entity with the name distanceFromCurrentLocation and you should be able to write a virtual accessors and have the NSFetchedResultsController see it.

殤城〤 2024-09-05 09:57:15

我尝试做同样的事情,按距某个位置的距离对结果进行排序。这是我的问题之一:

核心数据和核心位置

事实证明,你必须将所有位置加载到内存中以进行比较。我最终使用了 SQLite,它实现起来比较困难,但给我带来的此类功能的麻烦要少得多。

这可能不是您想要的答案,但如果您有一个大型数据集,我会推荐 SQLite。结果对我来说处理起来更快更容易。

I tried to do the same thing, to sort results by the distance from a location. Here's one of my questions:

Core Data and Core Location

It turned out that you have to load all the locations into memory to compare them. I ended up using SQLite, which is more difficult to implement but caused me far fewer headaches for this kind of function.

It's probably not the answer you want, but if you have a large data set, I'd recommend SQLite. It ended up being faster and easier to deal with for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文