xcode (NSFetchedResultsController): 无法读取缓存文件来更新存储信息时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39620217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(NSFetchedResultsController): couldn't read cache file to update store info timestamps
提问by iCanCode
I upgraded my project to Xcode 8. Now, I'm getting this error log with Xcode 8 and iOS 10 combination.
我将我的项目升级到 Xcode 8。现在,我收到了 Xcode 8 和 iOS 10 组合的错误日志。
Setting the cacheName to nil in the below code seems fix it.
在下面的代码中将 cacheName 设置为 nil 似乎可以解决它。
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?
我应该怎么做才能摆脱这个错误日志并在我的 FRC 中使用缓存?
回答by Donnit
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.
不应忽略此错误,因为它可能导致应用程序崩溃。它与文件描述符泄漏的 iOS 10 错误有关。有关于 openradar 和 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.
会发生什么:如果你使用 NSFetchedResultsController 加载一个带有非 nil cacheName 的视图控制器,每次你保存托管对象上下文时,你将打开一个或多个文件描述符,指向 fetchedResultsController 的 sectionInfo 缓存文件。这意味着,如果保存上下文 255 次,将达到设备上可以打开的最大文件数,并且可能无法打开新资源,从而导致任何后续打开 xib 文件、图像、数据库等失败。
The problem occurs also for apps already on production (built with xcode 7) on devices upgraded to iOS 10.
对于升级到 iOS 10 的设备上已经生产(使用 xcode 7 构建)的应用程序,也会出现此问题。
A temporary solution is disabling NSFetchedResultsController caching with nil as cacheName:
一个临时解决方案是禁用 NSFetchedResultsController 缓存,将 nil 作为缓存名称:
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.
显然,通过这种方式我们无法利用缓存。希望苹果尽快修复这个bug。我将针对 10.2 beta 1 进行测试。
EDITOn iOS 10.2 beta 1 the bug does not occur: it has been solved (for now).
编辑在 iOS 10.2 beta 1 上没有发生错误:它已经解决了(现在)。
回答by curieux
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.
我正在使用一个NSFetchedResultsController
. 然后我回去添加状态恢复。这个错误然后在恢复时开始出现。当我使用导航栏返回到以前的视图控制器时,数据全部丢失/不正确。
On reading the NSFetchedResultsController
docs, I discovered the following:
在阅读NSFetchedResultsController
文档时,我发现了以下内容:
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.
重要的
委托必须至少实现一种变更跟踪委托方法才能启用变更跟踪。提供一个空的实现
controllerDidChangeContent(_:)
就足够了。
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:
我只是controllerDidChangeContent(_:)
按照指示实施了一个空的。现在,一切正常,问题中的错误信息消失了。为了清楚起见,我只是在每个带有获取结果控制器的视图控制器中添加了以下代码:
// NSFetchedResultsController change tracking methods
func controllerDidChangeContent(_ controller:
NSFetchedResultsController<NSFetchRequestResult>) {
// empty: see documentation
}
Hope this helps.
希望这可以帮助。
回答by Saranjith
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.
在更加注意上述错误消息后,我可以看到您的应用程序正在创建大量文件描述符,在缓存文件夹中打开文件并且从未关闭或释放它们。所以最好暂时禁用NSFetchedResultsController
缓存。
Hope Apple will fix the issue
希望苹果能解决这个问题