NSFetchedResultsController v.s. UILocalizedIndexedCollation

Since you cannot sort on a transient property, the solution I implemented is…

  1. Create a string attribute called “sectionKey” for each sortable attribute within each entity in your Core Data model. The sectionKey attribute will be a calculated value derived from a base attribute (e.g., a name or title attribute). It must be persisted because (currently) a transient property cannot be used in a sort descriptor for a fetch request. Enable indexing on each sectionKey and base attribute for which sorting will be offered. In order to apply this update to an existing app, you will need to perform a lightweight migration, and also include a routine to update pre-existing databases.

  2. If you are seeding data (e.g., to populate new installs with a standard set of data, or to create localized SQLite databases for each target language, of which one will be copied over on initial launch), in that code, calculate and update each entity’s sectionKey attribute(s). Opinions vary as to the “best” approach to seeding data, however it’s worth noting that a handful of plist files for each language (which will typically range from a few bytes to 20k, even for a list comprised of several hundred values) will leave a much smaller overall footprint than an individual SQLite database for each language (which start at about 20k each). On a side note, Microsoft Excel for Mac can be configured to provide localized sorting of lists by enabling the language features (3).

  3. In the fetched results controller constructor, sort on the sectionKey and base attribute, and pass the sectionKey for the section name key path.

  4. Add the calculation logic to update the sectionKey attribute(s) in all add or edit user inputs, for example, in textFieldDidEndEditing:.

That’s it! No manual partitioning of fetched objects into an array of arrays. NSFetchedResultsController will do the localized collation for you. For example, in the case of Chinese (Simplified), the fetched objects will be indexed by phonetic pronunciation (4).

(1) From Apple IOS Developer Library > Internationalization Programming Topics > Internationalization and Localization.
(2) 3_SimpleIndexedTableView of the TableViewSuite.
(3) How to enable Chinese language features in Microsoft Office for Mac.
(4) The Chinese language is commonly sorted by either stroke count or phonetic pronunciation.

Leave a Comment