I am facing a really strange problem with Core Data. Let's describe it:
Let's say I have two models, ModelA
and ModelB
. In the data model ModelA
has a reference to ModelB
as a one-to-many association, and consequently ModelB
has a one-to-one association with ModelA
.
When the application launches (especially at first launch), or when the user asks, I have to create or update all the ModelB
instances for each ModelA
instance. ModelA
instances are predetermined. For each ModelA
instance I have about 200 instances of ModelB
.
I use a code like this:
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];
I do not persist data immediately (since I have lots of data to save), but I do it at the end of the process.
Sometimes, and only sometimes, I get an EXC_BAD_ACCESS
error at this line:
model.value = [NSNumber numberWithDouble:myValue];
Enabling the zombies, I cannot see nothing more than that stupid EXC_BAD_ACCESS
, no more info.
I tried to set that value in different ways, but nothing changed. Then, I tried to retain
the NSNumber
, but nothing changed. Then, finally, I tried to retain
the model once created and I got the usual EXC_BAD_ACCESS
, but correspondingly to the creation of the model, that is to say here:
ModelB *model = [[NSEntityDescription insertNewObjectForEntityForName:@"ModelB"
inManagedObjectContext:context] retain];
Have you got any ideas on how to solve this issue? Am I doing something wrong? By the way, this problem apparently does not occur if I slow things down a lot (eg. saving the context every time I create a new model), but this really slow the whole application ...
In some rare cases I get this stack trace:
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'
This is the stack trace of the exception:
#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 is not thread-safe; looks like that's your issue try looking at this SO question
I had a similar issue (EA_BAD_ACCESS on [managedContext save])which was caused by a KeyValueObserver that had not been removed when it should have been. It was quite hard to track down. This meant that I was getting this sort of behaviour even with ARC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With