我有一个堆栈跟踪显示响应内存警告的崩溃:
Thu Oct 28 00:42:55 iPhone DTMobileIS[10853] : _memoryNotification : {name = (null), num = 1} Thu Oct 28 00:42:55 iPhone DTMobileIS[10853] : _memoryNotification : { OSMemoryNotificationLevel = 1; timestamp = "2010-10-28 07:42:55 GMT"; } Thu Oct 28 00:42:55 iPhone MyApp[11059] : Received memory warning. Level=1 Thu Oct 28 00:42:55 iPhone MyApp[11059] : -[__NSCFType tryLock]: unrecognized selector sent to instance 0x41bfd0 Thu Oct 28 00:42:55 iPhone MyApp[11059] : *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType tryLock]: unrecognized selector sent to instance 0x41bfd0' *** Call stack at first throw: ( 0 CoreFoundation 0x30897ed3 __exceptionPreprocess + 114 1 libobjc.A.dylib 0x3002f811 objc_exception_throw + 24 2 CoreFoundation 0x30899683 -[NSObject(NSObject) doesNotRecognizeSelector:] + 102 3 CoreFoundation 0x308411d9 ___forwarding___ + 508 4 CoreFoundation 0x30840f90 _CF_forwarding_prep_0 + 48 5 CoreData 0x316cc261 -[_NSSQLCoreConnectionObsever _purgeCaches:] + 124 6 Foundation 0x349bb6b3 _nsnote_callback + 142 7 CoreFoundation 0x3081e713 __CFXNotificationPost_old + 402 8 CoreFoundation 0x3081e3b3 _CFXNotificationPostNotification + 118 9 Foundation 0x349aadb7 -[NSNotificationCenter postNotificationName:object:userInfo:] + 70 10 Foundation 0x349b42d1 -[NSNotificationCenter postNotificationName:object:] + 24 11 UIKit 0x31ed9431 -[UIApplication _performMemoryWarning] + 48 12 UIKit 0x31ec7383 -[UIApplication _receivedMemoryNotification] + 126 13 UIKit 0x31ec72c7 _memoryStatusChanged + 42 14 CoreFoundation 0x3084e825 __CFNotificationCenterDarwinCallBack + 24 15 CoreFoundation 0x30835313 __CFMachPortPerform + 210 16 CoreFoundation 0x3082cce3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26 17 CoreFoundation 0x3082cca7 __CFRunLoopDoSource1 + 166 18 CoreFoundation 0x3081f56d __CFRunLoopRun + 520 19 CoreFoundation 0x3081f277 CFRunLoopRunSpecific + 230 20 CoreFoundation 0x3081f17f CFRunLoopRunInMode + 58 21 GraphicsServices 0x31e445f3 GSEventRunModal + 114 22 GraphicsServices 0x31e4469f GSEventRun + 62 23 UIKit 0x31e51123 -[UIApplication _run] + 402 24 UIKit 0x31e4f12f UIApplicationMain + 670 25 MyApp 0x00052c81 main + 72 26 MyApp 0x00002f18 start + 52 )
我不太确定核心数据正在做什么,它试图从该对象获取锁,但是我觉得这表明我的代码中某处存在线程问题。
有人能想出一个好方法来调试这个问题或可能的根本原因吗?
最佳答案
我最近遇到了这个问题并找到了解决方案。该错误存在于 Core Data 本身中,并在 iOS 5.1 之前的版本中出现(似乎已在该版本中修复)。
当持久性存储协调器 (PSC) 无法加载给定的持久性存储(比如架构发生更改)时,就会发生崩溃。在这种情况下,失败的存储似乎仍然被添加到内部核心数据缓存中,即使没有外部访问它。当应用程序处于后台或在某些内存压力情况下,核心数据会自动刷新此缓存。一切都很好,除非用于尝试加载失败存储的 PSC 已被释放,在这种情况下,缓存将尝试访问它并给出上述错误。
总而言之,在这种情况下您会遇到此错误:
- 尝试从磁盘加载持久存储,但失败。发生这种情况的原因可能是您升级了应用并且模型架构发生了更改。
- 取消分配您用来尝试加载存储的持久存储协调器。
- 将应用置于后台,或在某些情况下触发内存警告。
- 崩溃
要解决此问题,有两种选择。我选择在 -addPersistentStoreWithType:configuration:URL:options:error: 调用失败的情况下不取消分配 PSC,在这种情况下,缓存清除仍将访问有效的 PSC。另一种方法是使用以下两个调用预先检查模型架构是否与磁盘上持久存储的架构相匹配:
+[NSPersistentStore metadataForPersistentStoreWithURL:error:]
-[NSManagedObjectModel isConfiguration:compatibleWithStoreMetadata:]
第二种解决方案可以避免在某些罕见情况下泄漏 PSC,但也假设持久存储加载失败的唯一可能原因是模型架构不匹配。
关于iphone - 核心数据 : Crash purging caches after a Memory warning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4047010/