問題描述
我的 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 按距離排序的核心數據中的位置?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!