gpt4 book ai didi

objective-c - 核心数据 : solve a strange EXC_BAD_ACCESS error

转载 作者:太空狗 更新时间:2023-10-30 03:40:18 27 4
gpt4 key购买 nike

我在使用 Core Data 时遇到了一个非常奇怪的问题。让我们来描述一下:

定义

假设我有两个模型,ModelAModelB。在数据模型中,ModelA 引用了 ModelB 作为一对多关联,因此 ModelB 具有一对一关联与 ModelA 的关联。

更新

当应用程序启动时(尤其是首次启动时),或者当用户要求时,我必须为每个 ModelA 实例创建或更新所有 ModelB 实例。 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,我什么也看不到,没有更多信息。

真正奇怪的错误

我尝试以不同的方式设置该值,但没有任何改变。然后,我尝试保留 NSNumber,但没有任何改变。然后,最后,我尝试保留创建后的模型,我得到了通常的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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com