Basic data: fix the strange error EXC_BAD_ACCESS

I ran into a really weird issue with Core Data. Describe this:

Definitions

Let's say I have two models: ModelAand ModelB. The data model ModelArefers to ModelBhow a one-to-many association is and therefore ModelBhas a one-to-one relationship with ModelA.

Update

When the application starts (especially the first time it starts) or when the user asks, I have to create or update all instances ModelBfor each instance ModelA. ModelAinstances are predefined. For each instance ModelA, I have about 200 copies ModelB.

I am using 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];

( ), .

, 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 ()
+5
4

CoreData ; , SO

+9

(EA_BAD_ACCESS [managedContext save]), KeyValueObserver, , . . , ARC.

+5

Reset

. , Core Datas migratePersistentStore:toURL:options:withType:error:.

. , , , managedObjectContext.reset() , .

, existingObjectWithID:error: , EXC_BAD_ACCESS. .

+1

, , :

model.modelA = modelA; // I pass modelA as a parameter to the function
[modelA addModelBObject:model];

, . , , . , . :

model.modelA = modelA;

... .

0

All Articles