問(wèn)題描述
我的 iOS Core Data 數(shù)據(jù)庫(kù)中有一組實(shí)體對(duì)象,用于描述某個(gè)位置的某些內(nèi)容.讓我們稱實(shí)體位置.我通過(guò)在 Location 上有兩個(gè)引用位置的屬性來(lái)實(shí)現(xiàn)這一點(diǎn) - 緯度和經(jīng)度,都是雙精度的.還有其他元素,例如名稱.
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 將實(shí)體綁定到 UITableViewController.我想做的是讓結(jié)果按到給定 CLLocationCoordinate2D 的距離排序.在一個(gè)非常理想的情況下,我可以刷新該列表以根據(jù)新位置重新計(jì)算排序.因此,這種排序?qū)⑷Q于兩個(gè)鍵和第三個(gè)靜態(tài)"變量(一個(gè)不會(huì)在集合中的項(xiàng)目之間變化的變量).
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 對(duì)任意列表進(jìn)行排序,我想我可以弄清楚如何做到這一點(diǎn).但是,我不控制排序描述符在 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.
有沒(méi)有一種方法可以配置我的實(shí)體、我的 NSFetchedResultsController、我的 NSSortDescriptors 等來(lái)完成此任務(wù)?我懷疑答案不在于創(chuàng)建一個(gè)花哨的 NSSortDescriptor,而是在實(shí)體中創(chuàng)建一個(gè)表示與我的距離的瞬態(tài)屬性,并定期重新計(jì)算該屬性.但是,我對(duì) Core Data 還很陌生,所以我不確定如何最好地做到這一點(diǎn)(遍歷所有實(shí)體并重新計(jì)算一個(gè)字段).我也不確定 NSSortDescriptors 是否適用于瞬態(tài)屬性.
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.
推薦答案
(來(lái)自評(píng)論:)
(基于 SQLite)Core Data 存儲(chǔ)的獲取請(qǐng)求不能使用基于瞬態(tài)屬性或基于 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.
如果您不想失去獲取結(jié)果控制器的優(yōu)勢(shì)(如動(dòng)畫表格視圖更新、自動(dòng)分組到部分等),您必須預(yù)先計(jì)算到當(dāng)前位置的距離并將其存儲(chǔ)在 (對(duì)象的持久性)屬性.
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.
或者,您可以獲取所有對(duì)象并在內(nèi)存中對(duì)其進(jìn)行排序.在這種情況下,您可以使用任意排序描述符.但這不能與獲取的結(jié)果控制器結(jié)合使用,因此您必須注冊(cè)托管對(duì)象上下文中的更改并在必要時(shí)重新加載表.
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.
這篇關(guān)于通過(guò) NSFetchedResultsController 按距離排序的核心數(shù)據(jù)中的位置?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!