如何为此查询设置谓词 [英] How to setup a predicate for this query

查看:113
本文介绍了如何为此查询设置谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3对象层次结构,它有一个根为Division,有很多'Group'项,每个'Group'有很多'Employees'。


部门[1] ------ [*]群组



群组[1] ------ [*]员工


我想知道如何创建一个NSPredicate来搜索特定部门的所有组的所有员工,按创建日期排序/ time,并通过他们的Group使用Employee对象作为请求实体并且知道特定部门的id来区分。



但我遇到了麻烦



我是构造一个SQL查询,它会是这样:



>

我试过,但没有工作:

  //为实体创建fetch请求。 
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];

//配置请求的实体及其谓词。
NSEntityDescription * entity = [NSEntityDescription
entityForName:@EmployeeinManagedObjectContext:context];
[fetchRequest setEntity:entity];

//创建谓词
NSPredicate * predicate = [NSPredicate predicateWithFormat:@SELF.groups IN%@,
[NSArray arrayWithArray:[[employee groups] allObjects] ]];

[fetchRequest setPredicate:predicate];

//根据需要编辑排序键。
NSSortDescriptor * createDateSortDcptor = [[NSSortDescriptor alloc]
initWithKey:@createDateTimeascending:YES];

NSArray * sortDescriptors = [[NSArray alloc]
initWithObjects:createDateSortDcptor,nil];

[fetchRequest setSortDescriptors:sortDescriptors];

我的对象层次结构如下:

  @interface部门:NSManagedObject 
{
}

@property(nonatomic,retain)NSDate * createDateTime;
@property(nonatomic,retain)NSSet * groups;

@end

@interface组:NSManagedObject
{
}

@property(nonatomic,retain)NSDate * createDateTime;
@property(nonatomic,retain)Division * division;
@property(nonatomic,retain)NSSet * employees;

@end

@interface Employee:NSManagedObject
{
}

@property(nonatomic,retain)NSDate * createDateTime;
@property(nonatomic,retain)NSSet * groups;

@end


解决方案

听起来像你想创建的谓词像

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

其中division是Division实例。这将获得与任何部门的组有关系的所有员工实例。



您的排序结果的代码看起来是正确的。



如果你希望有很多这样的员工,并且你想要批量获取结果,那么你使用获取请求的方法是正确的。如果你期望少数员工,你也可以使用键 - 值键编码:

  NSArray * divisionEmployees = [division valueForKeyPath:@@ unionOfSets.groups.employees]; 


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]------[*] Group

Group [1]------[*] Employee

I would like to know how to create an NSPredicate to search for all Employees of all Groups of a specific Division, sorted by creation date/time and sectioned in by their 'Group' using the 'Employee' object as the request entity and knowing the id of the specific Division.

But I'm having trouble translating this into CoreData so any suggestions would be helpful.

I were to construct a SQL query, it'd be something like this:

select * from Employee e where e.groupId in (select * from Group g where g.divisionId = :divisionId)

I tried this but didn't work:

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    // Configure the request's entity and its predicate.
    NSEntityDescription *entity = [NSEntityDescription 
entityForName:@"Employee" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    // Create the predicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.groups IN %@",
    [NSArray arrayWithArray:[[employee groups] allObjects]]];

    [fetchRequest setPredicate:predicate];

    // Edit the sort key as appropriate.
    NSSortDescriptor *createDateSortDcptor = [[NSSortDescriptor alloc] 
initWithKey:@"createDateTime" ascending:YES];

    NSArray *sortDescriptors = [[NSArray alloc] 
initWithObjects:createDateSortDcptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

My Object hierarchy is this:

@interface Division :  NSManagedObject  
{
}

@property (nonatomic, retain) NSDate * createDateTime;
@property (nonatomic, retain) NSSet* groups;

@end

@interface Group :  NSManagedObject  
{
}

@property (nonatomic, retain) NSDate * createDateTime;
@property (nonatomic, retain) Division * division;
@property (nonatomic, retain) NSSet* employees;

@end

@interface Employee :  NSManagedObject  
{
}

@property (nonatomic, retain) NSDate * createDateTime;
@property (nonatomic, retain) NSSet* groups;

@end

解决方案

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"];

这篇关于如何为此查询设置谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆