NSFetchedResultsController 部分本地化排序
如何将 NSFetchedResultsController 与翻译后的排序键和sectionKeyPath 一起使用?
问题:我在数据库中的属性“type”中有 ID,例如 typeA、typeB、typeC...,而不是直接的值,因为它应该本地化。在英语中,typeA=Bird,typeB=Cat,typeC=Dog,在德语中,则为 Vogel、Katze、Hund。
使用带有排序键和“类型”上的sectionKeyPath的NSFetchedResultController,我收到订单和部分 -A型 -B型 - typeC
接下来我翻译显示,一切都很好用英语: - 鸟 - 猫 - Dog
Now 我切换到德语并收到错误的排序顺序 ——沃格尔 - 卡茨 - Hund
因为它仍然按 typeA、typeB、typeC 排序
所以我正在寻找一种方法来本地化 NSFetchedResultsController 的排序。
我尝试了瞬态属性方法,但这对于排序键不起作用,因为排序键需要位于实体中。
我没有其他想法。但我无法相信不可能在本地化所需的派生属性上使用 NSFetchedResultsController 吗?
有类似的相关讨论 Using customsections with NSFetchedResultsController? 但不同之处在于自定义节名称和排序键可能具有相同的顺序。就我而言不是这样,这是主要的区别。
我想,最后我需要一个派生属性上必要的 NSSortDescriptor 的排序顺序。此排序顺序也适用于sectionKeyPath。
感谢您的任何提示。
How could I use the NSFetchedResultsController with translated sort key and sectionKeyPath?
Problem: I have ID in the property "type" in the database like typeA, typeB, typeC,... and not the value directly because it should be localized. In English typeA=Bird, typeB=Cat, typeC=Dog in German it would be Vogel, Katze, Hund.
With a NSFetchedResultController with sort key and sectionKeyPath on "type" I receive the order and sections
- typeA
- typeB
- typeC
Next I translate for display and everything is fine in English:
- Bird
- Cat
- Dog
Now I switch to German and receive a wrong sort order
- Vogel
- Katze
- Hund
because it still sorts by typeA, typeB, typeC
So I'm looking for a way to localize the sort for the NSFetchedResultsController.
I tried the transient property approach, but this doesn't work for the sort key because the sort key need to be in the entity.
I have no other idea. But I can't believe that's not possible to use NSFetchedResultsController on a derived attribute required for localization?
There are related discussions like Using custom sections with NSFetchedResultsController? but the difference is that the custom section names and the sort key have probably the same order. Not in my case and this is the main difference.
At the end I would need a sort order for the necessary NSSortDescriptor on a derived attribute, I guess. This sort order has also to serve for the sectionKeyPath.
Thanks for any hint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,这不是一个很好的解决方案,但最终它起作用了(因为我定义了大约 100 条有限的记录集):
在初始化应用程序时:
出于性能原因,我仅根据 NSPredicate 获取记录并对其进行排序。
我可以使用“order by”作为排序键和部分键路径来使用所有现有代码。
我知道我可以使用有序数组作为表视图的数据源,但我想保留现有代码并使用 NSFetchedResultsController 的方法。
,我可以完全控制排序,这将满足我将来的需求,因为我计划构建更复杂的排序顺序(基于位置、使用顶部记录的更高概率等)。
为了方便起见 这不是一个优雅的解决方案。
OK, not a nice solution but at the end it's working (because I have a defined limited set of records ca. 100):
On intializing the app:
For perfomance reason I only fetch and sort records according to a NSPredicate.
Than I can use all the existing code using the "order by" as sort key and section key path.
I know that I could use my ordered array as datasource for the table view, but I wanted to keep the existing code and use the methods of NSFetchedResultsController.
As a convenience of this I have full control over the sorting, that will fit my needs in future since I plan to build a more complex sort ordering (location based, higher probability of use of the records at the top, etc.)
However it's not an elegant solution.
对于排序,一种可能的做法是执行以下操作:
其中
translatedCompare
是您编写的比较方法(作为 NSString 上的类别),用于在比较值之前对值进行本地化。不确定如何处理sectionKeyPath。
For the sorting, one possibility would be to do something like:
where
translatedCompare
is a comparison method you write (as a category on NSString) that localizes the values before comparing them.Not sure about how to handle the sectionKeyPath.
我猜测问题是由缓存引起的。
当您使用以下方法创建 NSFetchedResultsController 对象时,您可以使用给定名称设置缓存。最后一个变量是缓存名称。
如果存在同名的缓存,则 NSFetchedResultsController 使用缓存来计算部分和顺序。并且缓存是写在磁盘上的(不是内存)。
因此,如果您在英语和德语之间更改语言,则应该删除缓存。要删除缓存,您可以使用类方法
deleteCacheWithName:
。您可以在这里找到详细信息。
http://developer.apple.com/iphone/library/documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40008227-CH1-SW24
I guess that the problem is caused by the cache.
You may set the cache with the given name when you create NSFetchedResultsController object by using the following method. The last variable is the cache name.
The NSFetchedResultsController uses the cache to calculate the sections and the ordering if the cache with the same name exists. And the cache is written on the disk (not memory).
So if you change the language between English and German, you should delete the cache. To delete the cache you can use the class method
deleteCacheWithName:
.You can find the detail information here.
http://developer.apple.com/iphone/library/documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40008227-CH1-SW24