NSFetchedResultsController部分本地化排序 [英] NSFetchedResultsController sections localized sorted

查看:65
本文介绍了NSFetchedResultsController部分本地化排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将NSFetchedResultsController与转换后的排序键和sectionKeyPath一起使用?

How could I use the NSFetchedResultsController with translated sort key and sectionKeyPath?

问题:我在数据库的类型"属性中有ID,例如typeA,typeB,typeC,...,而不是直接值,因为它应该本地化.在英语中,typeA = Bird,typeB = Cat,typeC = Dog,德语为Vogel,Katze,Hund.

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.

使用带有类型"上的排序键和sectionKeyPath的NSFetchedResultController,我收到订单和部分 -A型 -B型 -typeC

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

因为它仍然按typeA,typeB,typeC排序

because it still sorts by typeA, typeB, typeC

所以我正在寻找一种方法来本地化NSFetchedResultsController的排序.

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.

我没有其他想法.但是我无法相信无法在本地化所需的派生属性上使用NSFetchedResultsController吗?

I have no other idea. But I can't believe that's not possible to use NSFetchedResultsController on a derived attribute required for localization?

有相关的讨论,例如将自定义部分与NSFetchedResultsController一起使用?,但区别是自定义节名称和排序键可能具有相同的顺序.就我而言,这不是主要区别.

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.

最后,我想需要对派生属性上必要的NSSortDescriptor进行排序.这种排序顺序也必须用于sectionKeyPath.

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.

感谢任何提示.

推荐答案

好的,不是一个很好的解决方案,但是最后它就可以了(因为我定义了一组有限的记录,大约100条):

OK, not a nice solution but at the end it's working (because I have a defined limited set of records ca. 100):

初始化应用程序:

  • 我在托管对象中创建一个"order by"属性.
    • 我检查自上次以来本地化(及其排序顺序)是否发生了变化.如果是这样:
    • 我获取所有记录,并按本地化名称将其排序在NSArray中.
    • 我写回商店中的记录
    • I create an "order by" attribute in the managed object.
      • I check if the localization (and with it the sort order) changed since last time. If so:
      • I fetch all the records and sort it in an NSArray by localized names.
      • I write back the records in the store

      出于性能原因,我仅根据NSPredicate提取记录并对其进行排序.

      For perfomance reason I only fetch and sort records according to a NSPredicate.

      然后,我可以使用所有现有的代码,并使用"order by"作为排序键和分区键路径.

      Than I can use all the existing code using the "order by" as sort key and section key path.

      我知道我可以将有序数组用作表视图的数据源,但我想保留现有代码并使用NSFetchedResultsController的方法.

      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.

      这篇关于NSFetchedResultsController部分本地化排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆