我面临核心数据一个非常奇怪的问题。让我们描述一下:
定义
假设我有两个模型,ModelAModelB。在数据模型中,ModelAModelB称为一对多关联,因此ModelBModelA一对一关联。
更新
当应用程序启动时(尤其是在首次启动时),或者在用户询问时,我必须为每个ModelB实例创建或更新所有ModelA实例。 ModelA实例是预定的。对于每个ModelA实例,我大约有200个ModelB实例。
我使用这样的代码:

ModelB *model = [NSEntityDescription insertNewObjectForEntityForName:@"ModelB"
                                              inManagedObjectContext:context];
model.value = [NSNumber numberWithDouble:myValue];
model.modelA = modelA; // I pass modelA as a parameter to the function
[modelA addModelBObject:model];
我不会立即保存数据(因为我要保存很多数据),但是我会在过程结束时进行保存。
错误
有时,并且只有有时,我在此行收到EXC_BAD_ACCESS错误:
model.value = [NSNumber numberWithDouble:myValue];
启用僵尸,我只能看到那个愚蠢的EXC_BAD_ACCESS,仅此而已。
真正奇怪的错误
我试图以不同的方式设置该值,但没有任何改变。然后,我尝试对retain进行NSNumber,但是什么都没有改变。然后,最后,我尝试对创建的模型进行retain编码,然后得到了通常的EXC_BAD_ACCESS,但是对应于模型的创建,即在这里:
ModelB *model = [[NSEntityDescription insertNewObjectForEntityForName:@"ModelB"
                                               inManagedObjectContext:context] retain];
有想法吗?
您对如何解决此问题有任何想法吗?难道我做错了什么?
顺便说一句,如果我减慢很多速度(例如,每次创建新模型时都保存上下文),就不会出现这个问题,但这确实减慢了整个应用程序的运行速度...
编辑
在极少数情况下,我会得到以下堆栈跟踪:
Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  -[__NSCFSet addObject:]: attempt to insert nil with userInfo (null)
2011-06-15 11:36:59.864 myApp[457:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet addObject:]: attempt to insert nil'
*** Call stack at first throw:
(
0   CoreFoundation                      0x313dc64f __exceptionPreprocess + 114
1   libobjc.A.dylib                     0x34b3dc5d objc_exception_throw + 24
2   CoreFoundation                      0x313dc491 +[NSException raise:format:arguments:] + 68
3   CoreFoundation                      0x313dc4cb +[NSException raise:format:] + 34
4   CoreFoundation                      0x31351089 -[__NSCFSet addObject:] + 152
5   CoreData                            0x35136dd9 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processPendingUpdates:] + 524
6   CoreData                            0x350f4b3d -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 724
7   CoreData                            0x351363a5 -[NSManagedObjectContext processPendingChanges] + 16
8   CoreData                            0x350d027f _performRunLoopAction + 126
9   CoreFoundation                      0x313b3a35 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 16
10  CoreFoundation                      0x313b5465 __CFRunLoopDoObservers + 412
11  CoreFoundation                      0x313b675b __CFRunLoopRun + 854
12  CoreFoundation                      0x31346ec3 CFRunLoopRunSpecific + 230
13  CoreFoundation                      0x31346dcb CFRunLoopRunInMode + 58
14  GraphicsServices                    0x3658841f GSEventRunModal + 114
15  GraphicsServices                    0x365884cb GSEventRun + 62
16  UIKit                               0x368ded69 -[UIApplication _run] + 404
17  UIKit                               0x368dc807 UIApplicationMain + 670
18  myApp                               0x000028cf main + 82
19  myApp                               0x00002878 start + 40
)
terminate called after throwing an instance of 'NSException'
编辑2
这是异常的堆栈跟踪:
#0  0x313f1460 in __CFBasicHashAddValue ()
#1  0x3133fff8 in CFBasicHashAddValue ()
#2  0x31344162 in CFSetAddValue ()
#3  0x31351012 in -[__NSCFSet addObject:] ()
#4  0x3514211a in _PFFastMOCObjectWillChange ()
#5  0x3512ed46 in _PF_ManagedObject_WillChangeValueForKeyIndex ()
#6  0x35132e7e in _sharedIMPL_setvfk_core ()
#7  0x3513316a in _svfk_2 ()
#8  0x0003b750 in -[_TassoStorico setValoreValue:] (self=0x6d97bf0, _cmd=0x49064, value_=1.02600002) at _TassoStorico.m:87
#9  0x0001b62e in -[EuriborParser(hidden) readStoricoForzato] (self=0x74200d0, _cmd=0x48ff7) at EuriborParser.m:236
#10 0x31349f02 in -[NSObject(NSObject) performSelector:withObject:] ()
#11 0x000441c4 in -[MBProgressHUD launchExecution] (self=0x90a6ff0, _cmd=0x4b83f) at MBProgressHUD.m:482
#12 0x352b3388 in -[NSThread main] ()
#13 0x353255cc in __NSThread__main__ ()
#14 0x34e20310 in _pthread_start ()
#15 0x34e21bbc in thread_start ()

最佳答案

CoreData不是线程安全的;看起来这是您的问题,请尝试查看此SO question

关于objective-c - 核心数据 : solve a strange EXC_BAD_ACCESS error,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6355126/

10-09 16:25