核心数据 - 实体关系未按预期工作
我已经在 xcode 中设置了数据模型,如下所示
EntityA
AttA1 AttA2
实体
B AttB1 AttB2 AttB3
然后,我设置关系
EntityA
名称:rlpToEntityB
目的地:EntityB
反向:rlpToEntityA
到多个:已检查
EntityB
名称:rlpToEntityA
目的地:EntityA
反向:rlpToEntityB
到多个:未检查
EntityB
,即两者之间的关系,其中每个 EntityA 都可以有多个 我的理解是,如果我获取 EntityB 的子集,我就可以检索相关 EntityA 的值。
我有这个工作,以便我可以使用
NSManagedObject *objMO = [fetchedResultsController objectAtIndexPath:indexPath];
检索 EntityB 值strValueFromEntityB = [objMO valueForKey:@"AttB1"];
但是,如果我尝试通过执行以下操作从 EntityA 检索相关值
strValueFromEntityA = [objMO valueForKey:@"AttA1"];
我收到错误“实体 EntityB 与键 Atta1 的键值编码不兼容”
毫不奇怪,我想如果我切换事物以从 EntityA 获取,我无法访问 EntityB 的属性,因此看来定义的关系被忽略。
谁能发现我做错了什么吗?
我承认我对 iPhone 编程特别是 Core Data 非常陌生,所以请对我宽容一点,提供详细的解释或为我指出特定资源的方向。我已经下载了苹果示例应用程序(核心数据书籍、热门歌曲和食谱),但我仍然无法解决这个问题。
提前致谢, 内华达。
I have set up my data model in xcode like so
EntityA
AttA1
AttA2
EntityB
AttB1
AttB2
AttB3
I then set up the relationships
EntityA
Name: rlpToEntityB
Destination: EntityB
Inverse: rlpToEntityA
To Many: Checked
EntityB
Name: rlpToEntityA
Destination: EntityA
Inverse: rlpToEntityB
To Many: UnChecked
i.e. relationship between the two where Each one of EntityA can have many EntityB's
It is my understanding that if i fetch a subset of EntityB's I can then retrieve the values for the related EntityA's.
I have this working so that i can retrieve the EntityB values using
NSManagedObject *objMO = [fetchedResultsController objectAtIndexPath:indexPath];
strValueFromEntityB = [objMO valueForKey:@"AttB1"];
However, if I try to retrieve a related value from EntityA by doing the following
strValueFromEntityA = [objMO valueForKey:@"AttA1"];
I get the error "The entity EntityB is not Key value coding-compliant for the key Atta1"
Not surprisingly i suppose if i switch things around to fetch from EntityA i cannot access attributes of EntityB So it appears the defined relationshipare being ignored.
Can anyone spot what i am doing wrong?
I confess im very new to iPhone programming and especially to Core Data so please go easy on me and provide verbose explanations or point me in the direction a specific resource. I have downloaded the apple sample apps (Core Data Books, Top Songs and recipes) but I still can't work this out.
Thanks in advance,
Nev.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法直接从另一个实体获取一个实体的属性。借用你的术语,你应该这样做:(
实体 B)->(与 A 的关系)->(A 的属性)
或
You can't get attributes on one entity directly from another entity. To borrow your terminology, you should do something like this:
(Entity B)->(relationship to A)->(attribute of A)
or
非常感谢您的回复,但我现在已经解决了我自己的问题。虽然我确信您的回答有一定的道理,但这不是我用来解决问题的方法。 (也许我没有很好地解释它,因此缺乏响应)但简而言之,我需要使用 valueForKeyPath 方法而不是 valueForKey。
干杯,
内华达。
Thanks very much for your response but I have now solved my own problem. whilst i'm sure your response has some merit it's not what I used to solve my problem. (perhaps i didn't explain it very well hence the shortage of responses) but in short i needed to use the valueForKeyPath method instead of valueForKey.
Cheers,
Nev.