Core Data抛出NSInternalInconsistencyException“100次尝试后上下文仍然很脏。”

时间:2012-02-02 09:00:25

标签: 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 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 个答案:

答案 0 :(得分:9)

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

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

The context is still dirty after 100 attempts.

这就是它的要点。