Extra "inserts" sent to NSFetchedResultsControllerDelegate

I have an app where I have a FRC over a set of objects. If I update an object being "watched" by FRC, I'm getting an update notification AND an insert notification (for that object) in NSFetchedResultsControllerDelegate (didChangeObject). Because of this, core data asserts:


*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-3347.44.2/UITableView.m:1623
2015-07-20 23:55:26.623 mailapp[2432:2452516] CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (2 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)


I cannot figure out why I'm getting the extra insert. I tried overriding my model's "awakeFromInsert" - that is never hit. I've verified that only when I change a property on the model does the update and insert notification fire. I can insert and delete objects fine.


Update: In addition to FRC, I also subscribed to the NSManagedObjectContextObjectsDidChangeNotification event and from that event I only get one update, as expected. Why is FRC doing something funny?


Any ideas?

Replies

Same here, I guess it's ios9 bug...

ya same problem here any update to the coredata, and i get the follow error:


CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (12) must be equal to the number of rows contained in that section before the update (12), plus or minus the number of rows inserted or deleted from that section (2 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)


what i did ? nothing really just a simple update to the activity object when someone click on like button:

activity.liked = !activity.liked.boolValue

activity.likesCount = activity.likesCount.integerValue + 1


Radar fired with number: 22205376

Please verify if this issue persists in Xcode 7 beta 5.

It seems to be fixed in beta 5 in simulator and real device on ios9 beta, however, if I test it on real device with ios 8.4, it is still broken...

Piping in to say that I have the same issue on Xcode 7 beta 6. It works fine for me on iOS 9 on the simulator, but iOS 8.4 is not working on my device. Any updates on this?

Problem still there with the GM seed...

Are you sure about 22205376 number? Can't find it.

I'm having a similar issue with 8.4 (real device and simulator) when building with Xcode 7.

I'm having the same issue on iOS 8.4. Test on iOS 9.0 and it works fine. The problem is all my users are on 8.4.


Does anyone have any workarounds ?

This workaround turned out to be the solution for my issue:

https://forums.developer.apple.com/thread/4999#53526


Essentially, on iOS 8 only (iOS 9 worked fine), NSFetchedResultsControllerDelegate's didChangeObject was getting called a second time after a change, but the passed NSFetchedResultsChangeType was invalid (0x0). As most people seem to be using a switch on the type to handle various cases, this invalid type always triggered the first case result (which is an insert based on most code examples floating around).


You could either rearrange your case statemtents so that an Update is first (or some case that isn't destructive), or you could handle the invalid case first, as I do here:

switch(type) {
    case NSFetchedResultsChangeType(rawValue: 0)!:
        // iOS 8 bug - Do nothing if we get an invalid change type.
        break;

    case .Insert:
        // Insert stuff
        break;

    case .Delete:
        // Delete stuff
        break;

    case .Update:
        // Update stuff
        break;

    case .Move:
        // Move stuff
        break;
}

Same issue here, tested on iOS 8.3, 8.4 but not on iOS 9.0, iOS 9.1

Oh wow, what a horrible horrible bug, and not really mentioned in many places. This was causing all of my ios 8.4 clients to break.

same crash heppend here, you should check this

http://stackoverflow.com/questions/32318976/nsfetchedresultscontroller-xcode-7-issue

How is this not more upvoted??

We too experience the same error on iOS 9 using Xcode 7.3,


Can someone from Apple please weigh in on what's going on here?


We are using FRC with a UICollectionView. We have another app using a table view, and we don't see this issue.


Are there any recommendations around using FRC with a collection view that might be different than a table view? Any suggestions about the best way to collect updates and call perform batch updates?