I’m using CoreData in an iOS app to save a simple list of location objects (name, coordinates). I’ve run into a situation twice now, after a memory warning, when the CoreData records appear to become corrupt.
Specifically, (in tableView:numberOfRowsInSection:)
NSUInteger numObjects = [[[fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
returns 4 for section == 0.
Then (in tableView:cellForRowAtIndexPath:)
aLocation = [fetchedResultsController objectAtIndexPath:indexPath];
for indexPath [0,3] generates an exception:
NSRangeException: * -[_PFArray objectAtIndex:]: index (3) beyond bounds (3)
i.e. CoreData says there are 4 rows in a table, but then can’t return an object for the last row when the table view requests it.
Two questions:
1) Any thoughts on how this might arise and how to prevent it
2) Once it has occurred, is there any means to fix the data problem in code?
Thanks
Stephen
The issue appears to have been some inconsistency in the CoreData cache that was causing this – unclear what causes it, as the predicates are never changed. I ended up calling deleteCacheWithName if an exception is ever raised on accessing the last object in the fetchedResultsController. This appears to have resolved the issue (no issues from users in 6 months on this).