开发者

iPhone Development - Query related records using CoreData

I have a case where i have three entities with one-to-many a开发者_如何学Gond one-to-many relationships:

Entity A (Entity B relationhip), 
Entity B (Entity A relationship, Entity C relationship),
Entity C (Entity B relationhip)

I have the reference of Entity A, and now i want to fetch all the related Entity C records. How can i do that? (with least amount of code)

Edit: Here's another way to put it.

Can we perform joins with CoreData. For example, (and this is a very crude example), We have a following entity-relationship:

Grand Parent  (1)---(m)  Parent
Parent        (1)---(m)  Child

So, now if i have "Albert" the Grand Parent, and i want to get all his grand children, how can i do that?


In case someone else stumble across a similar situation, here's what worked for me:

NSArray *allFieldValues = [myEntityA valueForKeyPath:@"Entity B relationship.Entity C relationship.requiredFieldInEntityC"];

I was mainly interesting in reading the data of a single field in Entity C (that's linked to myEntityA object). The key concept here is that "don't think of CoreData as a 'database'".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜