How to setup a predicate for this query

后端 未结 1 1351
予麋鹿
予麋鹿 2021-01-16 14:06

I have an 3 object hierarchy which has a root as \'Division\' that has many \'Group\' items and each \'Group\' has many \'Employees\'.

Division [1]------

相关标签:
1条回答
  • 2021-01-16 14:32

    It sounds like you want to create the predicate like

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.groups IN %@",
                                                              [division groups]];
    

    where division is a Division instance. This will get all eployee instances that have a relationship to any of the division's groups.

    Your code to sort the results looks correct.

    If you expect to have many such employees, and you want to fetch the result in batches, then your approach using a fetch request is correct. If you're expecting a small number of employees, you can also get them using key-valye coding:

    NSArray *divisionEmployees = [division valueForKeyPath:@"@unionOfSets.groups.employees"];
    
    0 讨论(0)
提交回复
热议问题