开发者

NSFetchedResultsController sections localized sorted

开发者 https://www.devze.com 2022-12-28 13:23 出处:网络
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

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

be开发者_Python百科cause 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.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号