Question

In the following code:

NSFetchedResultsController *frc =
[[NSFetchedResultsController alloc]
 initWithFetchRequest:fetchRequest
 managedObjectContext:myManagedObjectContext
 sectionNameKeyPath:@"store"
 cacheName:@"SomeCache"
 ];

The @"store" value for sectionNameKeyPath actually points to a relationship to a Store entity which has a name attribute that I really need as my section header title.

But the way my code is setup it cannot be accomplished as I instead get section titles like: 0x5b504f0 0x5b51190 Which are code data addresses for the Store object being fetched as far as I can tell.

How can I use NSFetchedResultsController so that I may tell it that I want it to fetch the name attribute of whatever it fetches from sectionNameKeyPath:@"store" ? Or is there some other workaround?

Was it helpful?

Solution

Try sectionNameKeyPath:@"store.name"

OTHER TIPS

I know this is an old thread, but I would like to add my solution using Swift.

I must advice that this solution depends on the format of the section name returned by the NSFetchedResultsController, so it depends on internal implementations that can be changed by Apple.

The section name contains the URI of the object ID in the Persistent Data Store, so you can get the object by first extracting the URI and then asking the Store for the object with the corresponding URI

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let sectionInfo = fetchedResultsController?.sections![section]
    let sectionName = sectionInfo?.name

    //This part depends on the internal implementation of sectionInfo.name
    let initialRange = sectionName?.rangeOfString("<")
    let finalRange = sectionName?.rangeOfString(">")

    let uri = sectionName?.substringWithRange((initialRange?.endIndex)!..<(finalRange?.startIndex)!)
    let url = NSURL(string: uri!)

    let store = fetchedResultsController?.managedObjectContext.persistentStoreCoordinator

    let objectID = store?.managedObjectIDForURIRepresentation(url!)
    let coreObject = fetchedResultsController?.managedObjectContext.objectWithID(objectID!)
    //return the correct property from the object as the title
    let title = ...
    return title
}

Well, you need to check for errors (I would use a lot of guard in front of those let) but you get the idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top