13

I'm using NSFetchedResultsController (NSFRC) to display information in a UITableView. I'm trying to create the option for the user to sort the cells in sections as opposed to alphabetically. The problem is, the sections would then be determined using downloaded information. On top of this the section for each item will be changing relatively often so I don't want to save the section. I have noticed the mention of transient attributes, in my research of similar problems, but i've never used these before I'm not sure if I can use them baring in mind that all the calculations are done once the data has already been loaded, and I also want this solution to be compatible with my previous Core Data database. Also I'm not particularly great at Core Data, (nor Objective-C at that!) so I'm not entirely sure how I'd go about doing this.

So here's what I want to go for if we're using transient attributes (this next bit is theoretical as I don't know if transient attributes are the correct way forward). I would like 4 possible sections, 0-3 (I'll rename them using the TableView delegate to get around sorting problems). When the calculations are done, each cell will be assigned the transient attribute (if needed, the default section would be 2). I hope this all makes sense.

Right, now for some theoretical code. First I create the transient property in the Data Model screen-thing, and make it transient by checking the transient check box... Sounds simple enough.

In the code for the calculations in willDisplayCell (needs to be done in wDC for a couple of reasons), the entity could be saved like this:

MyEntity *myEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];

myEntity.sectionTransientProperty = 2;

if (![self.managedObjectContext save:&error]) {
    NSLog(@"Error: %@", error);
    FATAL_CORE_DATA_ERROR(error);
    return;
}  

Done, right? Is that how we assign a value to a transient property?

Then I change the sorting option in NSFRC when I alloc it:

fetchedResultsController = [[NSFetchedResultsController alloc]
                                initWithFetchRequest:fetchRequest
                                managedObjectContext:self.managedObjectContext
                                sectionNameKeyPath:@"sectionTransientProperty"
                                cacheName:@"MyEntity"];

How are we doing, what else do I need to do? Or have I got this so horribly wrong I should just give up on Core Data and NSFRC? If you guys could help guide me through this I'd really appreciate it. If you need me to post any more code I would be happy to.

Regards,
Mike

mccc
  • 2,354
  • 1
  • 20
  • 22
Mackey18
  • 2,322
  • 2
  • 25
  • 39

1 Answers1

28

If you want an FRC with sections, you have to add a sort descriptor to the fetch request, and that sort descriptor cannot be based on transient attributes.

See the documentation of initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:`:

If the controller generates sections, the first sort descriptor in the array is used to group the objects into sections; its key must either be the same as sectionNameKeyPath or the relative ordering using its key must match that using sectionNameKeyPath.

and Fetch Predicates and Sort Descriptors in the "Core Data Programming Guide":

The SQL store, on the other hand, compiles the predicate and sort descriptors to SQL and evaluates the result in the database itself. This is done primarily for performance, but it means that evaluation happens in a non-Cocoa environment, and so sort descriptors (or predicates) that rely on Cocoa cannot work. The supported sort selectors are ...
In addition you cannot sort on transient properties using the SQLite store.

This means that you cannot create sections purely on transient attributes. You need a persistent attribute that creates the ordering for the sections.

UPDATE: A typical use of a transient attribute as sectionNameKeyPath is: Your objects have a "timeStamp" attribute, and you want to group the objects into sections with one section per month (see the DateSectionTitles sample code from the iOS Developer Library). In this case you have

  • a persistent attribute "timeStamp",
  • use "timeStamp" as first sort descriptor for the fetch request,
  • a transient attribute "sectionIdentifier" which is used as sectionNameKeyPath. "sectionIdentifier" is calculated from "timeStamp" and returns a string representing the year and the month of the timestamp, e.g. "2013-01".

The first thing the FRC does is to sort all fetched objects according to the "timeStamp" attribute. Then the objects are grouped into sections according to the "sectionIdentifier" attribute.

So for a FRC to group the objects into sections you really need a persistent attribute. The easiest solution would be to add a persistent attribute "sectionNumber" to your entity, and use that for "sectionNameKeyPath" and for the first sort descriptor.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • I'm already using the name of the entity to sort alphabetically, I heard however that using `sectionNameKeyPath:@"sectionTransientProperty"` allows you to set sections from a transient. – Mackey18 Jan 05 '13 at 12:26
  • @Mackey18: The first sort descriptor must use the same key as the sectionNameKeyPath or the relative ordering using its key must match that using sectionNameKeyPath. - Is this condition satisfied? – Martin R Jan 05 '13 at 13:02
  • Hmm, so how did they do it here: http://stackoverflow.com/questions/1384345/using-custom-sections-with-nsfetchedresultscontroller ? Ok so if Transient Properties are a no go, how can I achieve what I was hoping? – Mackey18 Jan 05 '13 at 13:12
  • @Mackey18: I have updated the answer with more information how persistent and transient attributes work with FRC sections. And I think the same is done in http://stackoverflow.com/questions/1384345/using-custom-sections-with-nsfetchedresultscontroller: They use a transient attribute which is calculated from some persistent attribute. – Martin R Jan 05 '13 at 13:35
  • This is great Martin, thanks for clearing all that up for me. +1. However, the problem still remains and I'm none the wiser as to how I'm going to go about doing what I want. If transients are out of the question, baring in mind that my sections have nothing to do with TimeStamps, then what should I do? – Mackey18 Jan 05 '13 at 15:04
  • @Mackey18: Well, I already suggested that you add a *persistent* attribute "sectionNumber". If you don't want a persistent attribute, then the only alternative would be *not to use a FRC*, i.e. fetch all objects into an array and group them into sections "manually". But that means a lot of additional code, because you loose all the change notifications etc. – Martin R Jan 05 '13 at 15:09
  • Hmmm, ok, so would there be a way of converting any distributed versions of the application to compatible versions with regards to databases? – Mackey18 Jan 05 '13 at 15:20
  • In fact don't worry about answering that. I'll do some research and post another question if need be. Thanks for all the help. Regards, Mike. – Mackey18 Jan 05 '13 at 16:30
  • 1
    @Mackey18: According to [Lightweight Migration](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweightMigration.html#//apple_ref/doc/uid/TP40004399-CH4-SW1) in the "Core Data Model Versioning and Data Migration Programming Guide", Core Data can perform the addition of a new attribute with automatic data migration aka **lightweight migration**. – Martin R Jan 05 '13 at 16:31
  • Despite multiple answers found in other SO questions saying this could be done with a transient property or even just a computed property, I always ran into problems... this was the only solution that worked for me. Creating an additional stored property feels wrong but it's the only thing that consistently works. Thank you. – user3344977 Mar 01 '17 at 23:07
  • Besides the transient topic: Having the the section key as the first sortDescriptor as well is a major issue, otherwise the results will be mixed. – wider-spider Aug 20 '20 at 06:53