Core Data: 3 table join?

后端 未结 2 1865
情书的邮戳
情书的邮戳 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:05

    Is it possible for core data to add tests for attributes of both b and a? Or will I have to iterate over each result set successively to get my final result?

    select 
      p.id, p.total 
    from 
      purcord p, line l, delivery d 
    where 
      l.purcord_id = p.id
      and d.purcord_id = l.purcord_id
      and d.purcord_line_no = l.line_no
      and d.status = 'notdelivered'
      and l.status = 'open'
      and p.status = 'open'
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题