Core Data: 3 table join?

后端 未结 2 1866
情书的邮戳
情书的邮戳 2021-02-06 11:35

I know Core Data is not a database and there are many differences. Is this one?

In a database, I would commonly have the following

A ->> B ->> C

\"A\" h

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-06 12:25

    Your going about this backwards.

    First of all, you don't need the linking ids in Core Data because all related objects are already linked by the relationship. This means constructions like where a.id = b.aid and b.id = c.bid aren't needed at all.

    Secondly, you generally set the fetch entity for the entity that receives the defining test. In this case, that is c.attr="X"So, you set your fetch entity to C and your predicate should look something like:

    NSPredicate *p=[NSPredicate predicateWithFormat:@"attr=%@",xValue];
    

    This will return an array all the C instances that meet the test. Then finding any particular B or A is just a matter of walking the relationship for each C.

    If your inverse relationship is to-one e.g A<->>B<->>C, the you just ask each C for the value of the b.a so:

    AObject *anA = aCinstance.b.a;
    

    It's important to remember you are not dealing with tables here. You are dealing with an object graph. You set the fetch to a particular entity and then walk the filtered entities' relationships.

提交回复
热议问题