2

I followed this suggestion: Format sectionNameKeyPath NSFetchedResultsController - Swift

I'm having a bit of a problem with this implementation. Using Xcode 8 and iOS 10.

I set Codegen to Manual/None and added my NSManagedObject Subclasses.

Then I added as you suggested:

var sectionIdentifier: String
{
    get
    {
        let dateFormatter = DateFormatter()
        // dateFormatter.dateStyle = .short
        // dateFormatter.timeStyle = .none
        // ..or to stick to your original question:
        dateFormatter.dateFormat = "yyyy-MMM-dd"
        return dateFormatter.string(from: date! as Date)
    }
}

But for whatever reason it gets ignored by sectionNameKeyPath. When I enter "date" in sectionNameKeyPath I get the same result as AllReadyHome mentioned above.

I even tried:

return (date?.description)!

It didn't yield any results.

Maybe someone can point me in the right direction...

Update 1: That's what my fetchedResultsController look like:

let fetchRequest: NSFetchRequest<Time> = Time.fetchRequest()
    fetchRequest.fetchBatchSize = 20
    let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
    fetchRequest.sortDescriptors = [sortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "sectionIdentifier", cacheName: "Time")

Result: No sections No Sections

When using date, which is an attribute of my entity:

let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "date", cacheName: "Time")

It looks like this: Sections

Update 2: When using the sortDescriptor as suggested, I get a crash ("sectionIdentifier not in entity):

let fetchRequest: NSFetchRequest<Time> = Time.fetchRequest()
    fetchRequest.fetchBatchSize = 20
    let sortDescriptor1 = NSSortDescriptor(key: "sectionIdentifier", ascending: false)
    let sortDescriptor2 = NSSortDescriptor(key: "date", ascending: false)
    fetchRequest.sortDescriptors = [sortDescriptor1, sortDescriptor2]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "sectionIdentifier", cacheName: "Time")

Update 3: I get the following warning when getting to the screen:

A section returned nil value for section name key path 'sectionIdentifier'. Objects will be placed in unnamed section
 CoreData: error: (NSFetchedResultsController). A section returned nil value for section name key path 'sectionIdentifier'. Objects will be placed in unnamed section
  • use "sectionIdentifier" for the `sectionNameKeyPath` – Jon Rose Jun 13 '17 at 07:13
  • Sorry I didn't mention it, but I did. That's what does not work. –  Jun 13 '17 at 07:14
  • What exactly happens? is the fetchedResultsController returning sectioned results? how are you setting up the header for the view? – Jon Rose Jun 13 '17 at 07:33
  • How does your `NSFetchedResultsController` look? To be able to use `sectionNameKeyPath`, you need the same property as the first sort-descriptor – Audun Kjelstrup Jun 13 '17 at 11:41
  • Thanks for your help! ;-) please see updates above –  Jun 13 '17 at 14:41
  • Additionally, when checking 'Transient' in my data model, I get a crash, although I should check it, right? –  Jun 13 '17 at 14:43

1 Answers1

4

use @objc for your sectionIdentifier

Sample:

@objc var sectionIdentifier: String {
    get {
        return ""
    }
}
Tom
  • 41
  • 2