NSFetchedResultsController 部分本地化排序

发布于 2024-08-30 21:18:38 字数 907 浏览 4 评论 0原文

如何将 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 技术交流群。

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

发布评论

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

评论(3

舞袖。长 2024-09-06 21:18:38

好吧,这不是一个很好的解决方案,但最终它起作用了(因为我定义了大约 100 条有限的记录集):

在初始化应用程序时:

  • 我在托管对象中创建一个“order by”属性。
    • 我检查本地化(以及排序顺序)自上次以来是否发生了变化。如果是这样:
    • 我获取所有记录并按本地化名称在 NSArray 中对它们进行排序。
    • 我写回存储中的记录

出于性能原因,我仅根据 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:

  • 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

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.

巾帼英雄 2024-09-06 21:18:38

最后,我猜想,我需要对派生属性上必要的 NSSortDescriptor 进行排序。

对于排序,一种可能的做法是执行以下操作:

[NSSortDescriptor initWithKey:@"type" 
 ascending:YES 
 selector:@selector(translatedCompare:)]

其中 translatedCompare 是您编写的比较方法(作为 NSString 上的类别),用于在比较值之前对值进行本地化。

不确定如何处理sectionKeyPath。

At the end I would need a sort order for the necessary NSSortDescriptor on a derived attribute, I guess.

For the sorting, one possibility would be to do something like:

[NSSortDescriptor initWithKey:@"type" 
 ascending:YES 
 selector:@selector(translatedCompare:)]

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.

谢绝鈎搭 2024-09-06 21:18:38

我猜测问题是由缓存引起的。

当您使用以下方法创建 NSFetchedResultsController 对象时,您可以使用给定名称设置缓存。最后一个变量是缓存名称。

- (id)initWithFetchRequest:(NSFetchRequest *)fetchRequest managedObjectContext:(NSManagedObjectContext *)context sectionNameKeyPath:(NSString *)sectionNameKeyPath cacheName:(NSString *)name

如果存在同名的缓存,则 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.

- (id)initWithFetchRequest:(NSFetchRequest *)fetchRequest managedObjectContext:(NSManagedObjectContext *)context sectionNameKeyPath:(NSString *)sectionNameKeyPath cacheName:(NSString *)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

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