0

I'm using the Big Nerd Ranch BNRCoreDataStack, but I suspect that this problem would exist if I was using standard CoreData.

I have a FetchedResultsController to populate my table by fetching NSManagedObjects. These are being rendered in sections and rows. My problem is that when I delete my NSManagedObjects elsewhere, I get an error from the FetchedResultsController:

[error] error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  attempt to insert row 0 into section 1, but there are only 1 sections after the update with userInfo (null)

This leaves my cells visible in the table afterwards, and when I tap on one I get this error:

fatal error: Index out of range

So, this tells me that the FetchedResultsController has failed to remove the cells from my table when I deleted the CoreData objects.

What is strange is that I am reloading the table data on viewWillAppear so I would assume that that would clear the table. How can I force the FetchedResultsController to dump all knowledge of the data and start from scratch?

Lee Probert
  • 10,308
  • 8
  • 43
  • 70
  • You need to implement the FetchedResultController's delegate: http://stackoverflow.com/a/24983544/589224 – Patrick Tescher Jan 17 '17 at 22:47
  • I have. I believe this is where the problem originates: An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent – Lee Probert Jan 17 '17 at 22:49
  • I think this is because of the sorting of sections based on my object property. – Lee Probert Jan 17 '17 at 23:10

1 Answers1

0

I figured it out. My managed object is a Note class. Here it is:

@objc(Note)
class Note: NSManagedObject {

    @nonobjc override class func fetchRequest() -> NSFetchRequest{
        return NSFetchRequest(entityName: "Note");
    }

    @NSManaged var title: String?
    @NSManaged var body: String?
    @NSManaged var day: CalendarDay?
    @NSManaged var timestamp: NSDate?
}

extension Note: CoreDataModelable {

    static var entityName: String {

        return "Note"
    }

    var sectionTitle:String {

        willAccessValueForKey("day")
        defer { didAccessValueForKey("day") }

        if day != nil {

            return day!.timestampString!

        }else{
            return "___"
        }
    }
}

Notice I have this extra variable called sectionTitle ? Well, my FetchedResultsController was using that as its Section sorting key. The exception was happening when it tried to access the Day object, which had already been deleted.

The lesson learnt here, is not to try and add variables like this into your managed object classes, but make sure you have a managed attribute of your entity that you can use for sorting. It's managed for a good reason.

Lee Probert
  • 10,308
  • 8
  • 43
  • 70