开发者

iPhone Development - Query related records using CoreData

开发者 https://www.devze.com 2022-12-26 05:41 出处:网络
I have a case where i have three entities with one-to-many a开发者_如何学Gond one-to-many relationships:

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

精彩评论

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