So that I do not bury lede, I will open my main question: why is my NSFetchedResultsController fetchedObjects array usually homogeneous, but in rare cases contains __NSCFString among managed objects. must contain?
I have an application that has been running for a long time. This primary view is a tabular view that contains a list of videos supported by basic data-driven objects. The table view controller uses the NSFetchedResultsController configured with the fairly normal NSFetchRequest :
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:[ABCVideo entityName]]; NSString *sectionKeyPath = nil; request.fetchBatchSize = 20; NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:ABCVideoAttributes.recordingDate ascending:NO]; sectionKeyPath = @"sectionIdentifier"; request.sortDescriptors = @[sort]; request.predicate = [NSPredicate predicateWithFormat:@"owner = %@ and %K = %@", person, ABCVideoAttributes.serverDeleted, @(NO)]; self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:moc sectionNameKeyPath:sectionKeyPath cacheName:kABCMyVideosTableViewControllerCacheKey];
Since these videos can be uploaded to the cloud, this table view controller receives random notifications to update progress indicators in the table view cells corresponding to the videos that are currently being uploaded. Inside this callback, we get an array of NSFetchedResultsController fetchedObjects to find the video corresponding to the notification, so that the correct table view cell can update its progress bar.
It all works. 99.9% of the time, it works every time </RonBurgundy> .
But I noticed that in our HockeyApp crash report there is a rare, rare case where I got SIGABRT, which happens when my notification handler tries to get filteredArrayUsingPredicate from fetchedObjects :
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x136f24480> valueForUndefinedKey:]: this class is not key value coding-compliant for the key guid.'
I recently managed to find a case where I could sometimes reproduce this crash, and after a lot of experiments I found that the fetchedObjects array sometimes contained something that was not ABCVideo : instead, one slot in the array was occupied by an __NSCFString instance. This was quite surprising considering that NSFetchRequestResultType has NSManagedObjectResultType and the string is not a managed object.
So, I was left wondering: is this a Core Data error? Or does my array contain a pointer that previously pointed to an ABCVideo instance that was freed, and that the location on the heap was subsequently taken by the __NSCFString instance? If this is the last, then how could this happen? I use ARC, so itβs hard to understand how one of these videos can be released.