(NSFetchedResultsController): couldn't read ca

2019-02-01 17:18发布

问题:

I upgraded my project to Xcode 8. Now, I'm getting this error log with Xcode 8 and iOS 10 combination.

Setting the cacheName to nil in the below code seems fix it.

NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:NULL cacheName:@"myCache"];

What should I do to get rid of this error log and use cache in my FRC?

回答1:

This error should not be ignored because it can cause app crash. It is related to an iOS 10 bug of file descriptor leaks. There are reports on openradar and Apple Bug Reporter.

What happen: if you load a view controller using NSFetchedResultsController with a non-nil cacheName, every time you save the managed object context you will open one or more file descriptors pointing to the sectionInfo cache file of the fetchedResultsController. This means that if you save context 255 times, you will reach the maximum number of files that can be opened on devices and no new resources may be opened, causing any subsequent opening of xib files, images, database, etc. to fail.

The problem occurs also for apps already on production (built with xcode 7) on devices upgraded to iOS 10.

A temporary solution is disabling NSFetchedResultsController caching with nil as cacheName:

NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:NULL cacheName:nil];

Obviously in this way we can't get advantage of caching. I hope Apple will fix the bug asap. I am going to test against 10.2 beta 1.

OPEN RADAR 28361550

EDIT On iOS 10.2 beta 1 the bug does not occur: it has been solved (for now).



回答2:

First time I'm offering an answer here, but here goes...

I experienced this error and have found a resolution for my particular case.

I was using an NSFetchedResultsController. I then went back to add State Restoration. This error then began to appear upon restoration. When I used the navigation bar to go back to a previous view controller, the data were all missing/incorrect.

On reading the NSFetchedResultsController docs, I discovered the following:

Important

A delegate must implement at least one of the change tracking delegate methods in order for change tracking to be enabled. Providing an empty implementation of controllerDidChangeContent(_:) is sufficient.

I simply implemented an empty controllerDidChangeContent(_:) as directed. Now, everything works fine and the error message from the question is gone. To be clear, I simply added the following code in each view controller with a fetched results controller:

// NSFetchedResultsController change tracking methods
    func controllerDidChangeContent(_ controller: 
NSFetchedResultsController<NSFetchRequestResult>) {
        // empty: see documentation
    }

Hope this helps.



回答3:

After paying more attention to the above error message, I could see that your App was creating a large amount of File Descriptors, opening files in a cache folder and never closing or releasing them. So it better to disable the NSFetchedResultsController caching for now.

Hope Apple will fix the issue