未设置核心数据逆关系 [英] Core Data Inverse Relationship Not Being Set

查看:24
本文介绍了未设置核心数据逆关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体,我将它们称为 A 和 B.它们在两个方向上都配置了多对多关系,因此 A.myBs 和 B.myAs 都是 NSSet.

I've got two Entities that I'll call A and B. They are configured with To-Many relationships in both directions, so A.myBs and B.myAs are both NSSets.

这是我的奇怪问题.

当我向 A 实体添加 B 时,我使用 mutableSetValueForKey 这样做:

When I add a B to my A entity, I do it using the mutableSetValueForKey like this:

NSMutableSet *myBSet = [myA mutableSetValueForKey:@"myBs"];
[myBSet addObject:theBtoAdd];

这确实将 theBtoAdd 添加到 A 实体,但不会添加反向关系.核心数据上下文保存不会引发任何错误,但我的 A 对象没有 B 逆集.如果我退出应用程序,即使是部分关系也不会保存.

This does add the theBtoAdd to the A entity but does not add the inverse relationship. Core Data context save doesn't kick any errors, but my A object doesn't have the B inverse set. If I exit the application, even the partial relationship isn't saved.

这是奇怪的部分......如果我只是切换我的代码并做相反的事情(有一些原因为什么这对于我的特定应用程序来说更难做到) - 将 A 添加到 B 而不是像这样将 B 添加到 A:

Here's the strange part... if I just switch my code around and do the opposite (there are reasons why this is harder to do for my particular application) - add A to B instead of adding B to A like this:

NSMutableSet *myASet = [myB mutableSetValueForKey:@"myAs"];
[myASet addObject:theAtoAdd];

它工作得很好.顺便说一下,我还有很多其他的多对多关系.只是这个没有.

It works just fine. By the way, I have plenty of other to-many relationships that work. Just this one doesn't.

其他几件事:1) 我的核心数据对象模型看起来不错,但这是我在 Xcode 4 下添加的第一个新实体2)我已经检查,重新检查并盲目地查看我的自定义 NSManagedObjects,但它们看起来很好 - 声明为动态,NSSet,没有冲突的 setter/getter ......等等.

Couple of other things: 1) My core data object model looks good, but this is the first new entity that I've added under Xcode 4 2) I've check, rechecked and gone blind looking at my custom NSManagedObjects, but they look fine - declared dynamic, NSSet, no conflicting setters/getters... etc.

任何帮助或调试建议将不胜感激.谢谢!

Any help or debugging suggestions would be greatly appreciated. Thank you!

推荐答案

我刚遇到同样的问题,所以才看到这个问题.也许我的解决方案会帮助一些处于同样情况的人.

I just had the same problem, which was why I saw this question. Maybe my solution will help some people in the same situation.

对我来说,答案是在 NSManagedObject 子类中我已经覆盖了:

For me the answer was that in the NSManagedObject subclass I had over-riden:

- (void)willChangeValueForKey:(NSString *)inKey withSetMutation:(NSKeyValueSetMutationKind)inMutationKind usingObjects:(NSSet *)inObjects

- (void)didChangeValueForKey:(NSString *)inKey withSetMutation:(NSKeyValueSetMutationKind)inMutationKind usingObjects:(NSSet *)inObjects

这会阻止向处理自动反向关系的父类 (NSManagedObject) 发送正确的通知.

This prevents the correct notifications being sent to the parent class (NSManagedObject) which handles the automatic reverse relationships.

这篇关于未设置核心数据逆关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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