不同上下文中对象之间的非法关系:但我只有一个上下文? [英] Illegal relationship between objects in different contexts: but I only have one context?

查看:131
本文介绍了不同上下文中对象之间的非法关系:但我只有一个上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:


'NSInvalidArgumentException',原因:
非法尝试建立$ b $运行我的iphone应用程序时,
对象在不同上下文中的关系条件


我的模型的相关部分如下所示:

  AssessmentTree 
- 一个TreeCrown

TreeCrown
-has one TreeCrownCondition
- 一个AssessmentTree

TreeCrownCondition
- 许多TreeCrowns

为了上下文,我将提到模型的这一部分是为了允许树艺师记录树冠的状态。它存储用于评估树冠的选项列表,也可以由用户编辑(因此TreeCrownConditions包含选项,而TreeCrown.condition指向评估中做出的特定选择)。



我有一个UIPickerView加载这些选项,并使用didSelectRow中的以下代码将所选的选项与AssessmentTree关联:inComponent:

  TreeCrownCondition * fc =(TreeCrownCondition *)[conditionArray objectAtIndex:[conditionPicker selectedRowInComponent:0]]; 
tree.crown.condition = fc;

当我运行这个,前几次我选择一个选项一切都很好,但有时在添加/编辑/删除一个TreeCrownCondition选项后)应用程序将崩溃在上面的行与我在开头发布的错误。



错误似乎很容易修复, em>除了,我只在我的应用程序的整个使用一个NSManagedObjectContext。每个新控制器在加载以下代码时抓取它:

  if(!managedObjectContext){
managedObjectContext = [ (AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}

我找不到有关此错误或如何解决的很多信息,我当然没有找到任何人谁有这个问题,没有使用多个上下文。



我只能假设我有某种方式有多个上下文,但我看不到这是可能的。




$ b

编辑遇到堆栈跟踪:

> ***由于未捕获异常NSInvalidArgumentException而终止应用程序,原因:在不同上下文中的对象之间非法尝试建立关系条件(source =< TreeCrown:0x64b9f​​00>(entity:TreeCrown ; id:0x648db30 condition =0x64e5c50< x-coredata:// 6E17F271-7670- 44EA-9901-5AB58DCA8AC7 / TreeCrownCondition / p4>;
images =< relationship fault:0x64d5e10'images'>;
recommended =0x64a5320< x-coredata:// 6E17F271- 7670-44EA-9901-5AB58DCA8AC7 / TreeCrownRecommendation / P1>中,
树=0x6488f70< X-coredata:// 6E17F271-7670-44EA-9901-5AB58DCA8AC7 / AssessmentTree / P1>中,
}),目的地= LT; TreeCrownCondition:0xf218a40>(单位:TreeCrownCondition;编号:0xf215c20< X-coredata:// 6E17F271-7670-44EA-9901-5AB58DCA8AC7 / TreeCrownCondition / P2> ;数据:< fault>))'
***首次调用堆栈:

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风景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(静态)_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

在抛出一个'NSException'实例后调用


解决方案

如何创建实体?如果您创建的实体没有 NSManagedObjectContext ,它会产生相同的错误。



更新



从你的代码看,我猜你从选择器中得到的东西不是你期望的或不必要的。



我会在 objc_throw_exception 上放置一个断点,并在调试器中复制崩溃。一旦你这样做,然后检查对象,我希望你会发现你从选择器得到的不是一个 NSManagedObject ,但别的东西。


I get the following error:

'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'condition' between objects in different contexts

when running my iphone app. The relevant part of my model looks like this:

AssessmentTree
-has one TreeCrown

TreeCrown
-has one TreeCrownCondition
-has one AssessmentTree

TreeCrownCondition
-has many TreeCrowns

For the sake of context, I'll mention that this part of the model is designed to allow an arborist to record the condition of a tree's crown. It stores a list of options for assessing the tree crown that can also be edited by the user (So TreeCrownConditions contains the options, and TreeCrown.condition points to the specific selection made in an assessment).

I have a UIPickerView that loads these options and associates the selected option with the AssessmentTree, using the following code in didSelectRow:inComponent:

TreeCrownCondition *fc = (TreeCrownCondition *)[conditionArray objectAtIndex:[conditionPicker selectedRowInComponent:0]];
tree.crown.condition = fc;

When I run this, the first few times I select an option everything is fine, but sometimes (usually after adding/editing/deleting a TreeCrownCondition option) the app will crash on the above lines with the error I posted at the beginning.

The error seems straightforward to fix, except that I'm only using one NSManagedObjectContext throughout the entirety of my app. Each new controller grabs it when it's loaded with the following code:

if(!managedObjectContext){
    managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}

I couldn't find very much information about this error or how to resolve it, and I certainly didn't find anyone who'd had this problem without using multiple contexts.

I can only assume I somehow do have multiple contexts, but I can't see how that's possible. What am I missing?

Edit Heres the stack trace:

*** 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'

解决方案

How are you creating your Entities? If you are creating an entity without a NSManagedObjectContext it would produce the same error.

Update

From the look of your code I am guessing that what you are getting back out of the picker is not what you are expecting or unnecessarily casting to.

I would put a breakpoint on objc_throw_exception and duplicate the crash in the debugger. Once you do that then inspect the objects and I expect you will find that what you are getting out of the picker is not a NSManagedObject but something else.

这篇关于不同上下文中对象之间的非法关系:但我只有一个上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆