I have a UITableView
that uses an NSFetchedResultsController
as data source.
The core data store is updated in multiple background threads running in parallel (each thread using it's own NSManagedObjectContext
).
The main thread observes the NSManagedObjectContextDidSaveNotification
notification and updates it's context with mergeChangesFromContextDidSaveNotification:
.
Sometimes it happens that the NSFetchedResultsController
sends an
NSFetchedResultsChangeUpdate
event with an indexPath that does not exist
anymore at that point.
For example: The result set of the fetched results controller contains
1 section with 4 objects. The first object is deleted in one thread.
The last object is updated in a different thread. Then sometimes the
following happens:
- controllerWillChangeContent: is called.
- controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: is called with
type = NSFetchedResultsChangeDelete, indexPath.row = 0.
- controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: is called with
type = NSFetchedResultsChangeUpdate, indexPath.row = 3.
But the fetched results controller contains only 3 objects now, and if call
MyManagedObject *obj = [controller objectAtIndexPath:indexPath]
to update the table view cell according to the NSFetchedResultsChangeUpdate
event, this crashes with a NSRangeException
exception.
Thank you for any help or ideas!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…