开发者

Core Data - Entity Relationships Not Working as expected

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:ind开发者_开发技巧exPath];

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.


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

[[objMO valueForKey: @"rlpToEntityA"] valueForKey: @"AttA1"]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜