Macos 核心数据抛出“内部一致性异常”;经过100次尝试后,上下文仍然不干净。”;

Macos 核心数据抛出“内部一致性异常”;经过100次尝试后,上下文仍然不干净。”;,macos,cocoa,exception,core-data,Macos,Cocoa,Exception,Core Data,我很难找到以下异常的原因。它时不时地发生,并没有清晰的模式,我可以重复重现这个问题 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

我很难找到以下异常的原因。它时不时地发生,并没有清晰的模式,我可以重复重现这个问题

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

在保存对象时,您正在修改该对象。如果您正在观察
NSManagedObjectContextObjectsIDChange
(将作为保存的一部分进行发布,但在实际保存之前),并且您更改的对象是该通知的结果,则您将创建一个循环:

调用
-save:
CoreData时,首先调用
-processPendingChanges:
(如果有更改)。作为该CoreData的一部分,它会发出
nsManagedObjectContextObjectsIDChange
通知。如果在处理通知时更改了其他对象,则会再次调用
-processPendingChanges:
,再次发送通知等。一旦没有挂起的更改,CoreData会将对象持久化到存储区。如果挂起的更改继续出现,CoreData最终将放弃并打印

The context is still dirty after 100 attempts.

这就是它的要点。

您在保存对象时正在修改它。如果您正在观察
NSManagedObjectContextObjectsIDChange
(将作为保存的一部分进行发布,但在实际保存之前),并且您更改的对象是该通知的结果,则您将创建一个循环:

调用
-save:
CoreData时,首先调用
-processPendingChanges:
(如果有更改)。作为该CoreData的一部分,它会发出
nsManagedObjectContextObjectsIDChange
通知。如果在处理通知时更改了其他对象,则会再次调用
-processPendingChanges:
,再次发送通知等。一旦没有挂起的更改,CoreData会将对象持久化到存储区。如果挂起的更改继续出现,CoreData最终将放弃并打印

The context is still dirty after 100 attempts.
这就是要点