我收到以下错误:
'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'condition' between objects in different contexts
运行我的 iPhone 应用程序时。我的模型的相关部分如下所示:
AssessmentTree
-has one TreeCrown
TreeCrown
-has one TreeCrownCondition
-has one AssessmentTree
TreeCrownCondition
-has many TreeCrowns
出于上下文考虑,我将提到模型的这一部分旨在允许树艺师记录树冠的状况。它存储用于评估树冠的选项列表,也可以由用户编辑(因此 TreeCrownConditions 包含选项,TreeCrown.condition 指向评估中所做的特定选择)。
我有一个 UIPickerView,它加载这些选项并将所选选项与 AssessmentTree 关联起来,在 didSelectRow:inComponent 中使用以下代码:
TreeCrownCondition *fc = (TreeCrownCondition *)[conditionArray objectAtIndex:[conditionPicker selectedRowInComponent:0]];
tree.crown.condition = fc;
当我运行这个时,前几次我选择一个选项一切都很好,但有时(通常在添加/编辑/删除 TreeCrownCondition 选项之后)应用程序会在上面几行崩溃,并出现我在开头发布的错误.
这个错误似乎很容易修复,除了,我在整个应用程序中只使用了一个 NSManagedObjectContext 。每个新 Controller 在加载以下代码时都会捕获它:
if(!managedObjectContext){
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
我找不到关于此错误或如何解决它的太多信息,而且我当然没有找到任何人在不使用多个上下文的情况下遇到此问题。
我只能假设我确实有多个上下文,但我不明白这是怎么可能的。我错过了什么?
编辑这是堆栈跟踪:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'condition' between objects in different contexts (source = <TreeCrown: 0x64b9f00> (entity: TreeCrown; id: 0x648db30 <x-coredata://6E17F271-7670-44EA-9901-5AB58DCA8AC7/TreeCrown/p3> ; data: {
condition = "0x64e5c50 <x-coredata://6E17F271-7670-44EA-9901-5AB58DCA8AC7/TreeCrownCondition/p4>";
images = "<relationship fault: 0x64d5e10 'images'>";
recommendation = "0x64a5320 <x-coredata://6E17F271-7670-44EA-9901-5AB58DCA8AC7/TreeCrownRecommendation/p1>";
tree = "0x6488f70 <x-coredata://6E17F271-7670-44EA-9901-5AB58DCA8AC7/AssessmentTree/p1>";
}) , destination = <TreeCrownCondition: 0xf218a40> (entity: TreeCrownCondition; id: 0xf215c20 <x-coredata://6E17F271-7670-44EA-9901-5AB58DCA8AC7/TreeCrownCondition/p2> ; data: <fault>))'
*** Call stack at first throw:
(
0 CoreFoundation 0x029d5919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x027ea5de objc_exception_throw + 47
2 CoreData 0x02461810 _PFManagedObject_coerceValueForKeyWithDescription + 1248
3 CoreData 0x02478245 _sharedIMPL_setvfk_core + 197
4 CoreData 0x0247bfe7 _svfk_0 + 39
5 landscapes 0x0000e569 -[AssessmentTreeCRViewController pickerView:didSelectRow:inComponent:] + 571
6 UIKit 0x004647aa -[UIPickerView _sendSelectionChangedForComponent:] + 100
7 UIKit 0x00602ed3 -[UIScroller _scrollAnimationEnded] + 130
8 UIKit 0x0050e792 -[UIAnimator stopAnimation:] + 467
9 UIKit 0x0050e557 -[UIAnimator(Static) _advance:] + 298
10 GraphicsServices 0x034c856d HeartbeatTimerCallback + 35
11 CoreFoundation 0x029b6d43 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
12 CoreFoundation 0x029b8384 __CFRunLoopDoTimer + 1364
13 CoreFoundation 0x02914d09 __CFRunLoopRun + 1817
14 CoreFoundation 0x02914280 CFRunLoopRunSpecific + 208
15 CoreFoundation 0x029141a1 CFRunLoopRunInMode + 97
16 GraphicsServices 0x034c52c8 GSEventRunModal + 217
17 GraphicsServices 0x034c538d GSEventRun + 115
18 UIKit 0x0047cb58 UIApplicationMain + 1160
19 landscapes 0x00001eb6 main + 104
20 landscapes 0x00001e45 start + 53
)
terminate called after throwing an instance of 'NSException'
最佳答案
您如何创建实体?如果您创建的实体没有 NSManagedObjectContext
它会产生相同的错误。
更新
从代码的外观来看,我猜测您从选择器中返回的内容不是您所期望的或不必要的转换。
我会在 objc_throw_exception
上放置一个断点并在调试器中复制崩溃。一旦你这样做了,然后检查对象,我希望你会发现你从选择器中得到的不是 NSManagedObject
但还有别的事。
关于iphone - 不同上下文中对象之间的非法关系 : but I only have one context?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3505004/