Core Data 抛出 NSInternalInconsistencyException “100 次尝试后上下文仍然很脏。”
Posted
技术标签:
【中文标题】Core Data 抛出 NSInternalInconsistencyException “100 次尝试后上下文仍然很脏。”【英文标题】:Core Data throws NSInternalInconsistencyException "The context is still dirty after 100 attempts." 【发布时间】:2012-02-02 09:00:24 【问题描述】:我很难找到以下异常的原因。它不时发生,没有明确的模式我可以重复重现问题。
Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Failed to process pending changes before save.
The context is still dirty after 100 attempts.
Typically this recursive dirtying is caused by a bad validation method,
-willSave, or notification handler.'
应用程序存储和处理跟踪数据并且只处理一个实体:
@interface CSTrackingEntry : NSManagedObject
@property (nonatomic, retain) NSString * data;
@property (nonatomic, retain) NSDate * dateRecorded;
@end
CSTracking 条目是批量读取、创建和删除的。几到几十个,每几分钟一次。没有注册通知处理程序。
更新:堆栈捕获
2012-02-03 10:26:11.121 BatteryNurse[17162:1803] An uncaught exception was raised
2012-02-03 10:26:11.121 BatteryNurse[17162:1803] Failed to process pending changes
before save. The context is still dirty after 100 attempts. Typically this recursive
dirtying is caused by a bad validation method, -willSave, or notification handler.
2012-02-03 10:26:11.264 BatteryNurse[17162:1803] *** Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'Failed to process pending changes
before save. The context is still dirty after 100 attempts. Typically this recursive
dirtying is caused by a bad validation method, -willSave, or notification handler.'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff8183f784 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff89306f03 objc_exception_throw + 45
2 CoreData 0x00007fff8543a654 -[NSManagedObjectContext(_NSInternalChangeProcessing) _prepareForPushChanges:] + 244
3 CoreData 0x00007fff8543a0af -[NSManagedObjectContext save:] + 207
4 BatteryNurse 0x0000000100075ee6 __40-[CSTrackingEntry(Methods) deleteObject]_block_invoke_0 + 102
5 BatteryNurse 0x000000010007514f __42-[CSCoreDataKernel(CoreData) executeSync:]_block_invoke_0 + 79
6 libSystem.B.dylib 0x00007fff869bcfbb dispatch_barrier_sync_f + 79
7 BatteryNurse 0x00000001000750ee -[CSCoreDataKernel(CoreData) executeSync:] + 110
8 BatteryNurse 0x0000000100075e6f -[CSTrackingEntry(Methods) deleteObject] + 175
9 CoreFoundation 0x00007fff817ff123 -[NSArray makeObjectsPerformSelector:] + 499
10 BatteryNurse 0x000000010003a13f -[CSTracker(PrivateMethods) processAndSendBundlesToServer] + 383
11 BatteryNurse 0x00000001000393a4 __23-[CSTracker flushAsync]_block_invoke_0 + 420
12 libSystem.B.dylib 0x00007fff869c3d64 _dispatch_call_block_and_release + 15
13 libSystem.B.dylib 0x00007fff869a28d2 _dispatch_queue_drain + 251
14 libSystem.B.dylib 0x00007fff869a2734 _dispatch_queue_invoke + 57
15 libSystem.B.dylib 0x00007fff869a22de _dispatch_worker_thread2 + 252
16 libSystem.B.dylib 0x00007fff869a1c08 _pthread_wqthread + 353
17 libSystem.B.dylib 0x00007fff869a1aa5 start_wqthread + 13
【问题讨论】:
【参考方案1】:您在保存对象时正在修改它。如果您正在观察NSManagedObjectContextObjectsDidChange
(它将作为保存的一部分发布,但在实际保存发生之前),并且您更改对象是该通知的结果,您将创建一个循环:
当你调用-save:
时CoreData首先调用-processPendingChanges:
(如果有变化)。作为其中的一部分,CoreData 会发出 NSManagedObjectContextObjectsDidChange
通知。如果在处理通知时有其他对象发生更改,它会再次调用-processPendingChanges:
,再次发送通知等。一旦没有待处理的更改,CoreData 会将对象持久保存到存储中。如果未决的更改不断出现,CoreData 最终将放弃并打印
The context is still dirty after 100 attempts.
这就是它的要点。
【讨论】:
以上是关于Core Data 抛出 NSInternalInconsistencyException “100 次尝试后上下文仍然很脏。”的主要内容,如果未能解决你的问题,请参考以下文章
从 Core Data 打印 NSError 的 userInfo 会抛出 EXC_BAD_ALLOC
Swift、Core Data、可选的 Integer16 和 keyPath
为啥@angular/core/core 没有导出成员'ɵɵFactoryDeclaration'。构建项目时抛出错误?
Django 1.7 抛出 django.core.exceptions.AppRegistryNotReady:模型尚未加载