核心数据反向关系不设置 [英] Core Data Inverse Relationship Not Being Set

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

问题描述

我有两个实体,我将称为A和B.他们配置了两个方向上的To-Many关系,因此A.myBs和B.myAs都是NSSets。

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.

这是我的奇怪问题。

当我添加一个B到我的A实体,我使用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];

这会将BtoAdd添加到A实体,但不添加反向关系。 Core Data上下文保存不会引发任何错误,但是我的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:

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,没有冲突的setters / getters ...等。

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子类中我已经over-riden:

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天全站免登陆