我使用核心数据和hava 2 embadded relationship to-many。我也有一个日历,我应该在选定的日期下载所有疾病的所有药物。我如何使用子查询或使用子查询来完成它?
Disease ->>Medicine->>Date
屏幕截图位于http://savepic.ru/4608231.png
Couild我搜索所有没有疾病的药物?
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:@"MyMedicine" inManagedObjectContext:objectContext]];
[request setSortDescriptors:[NSArray initWithObject:[[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]];
[request setPredicate:[NSPredicate predicateWithFormat:@"(data == %@)", currentData]];
答案 0 :(得分:0)
假设你像这样初始化你的NSFetchRequest:
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:@"MyMedicine" inManagedObjectContext:objectContext]];
[request setSortDescriptors:[NSArray initWithObject:[[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]];
您可以像这样设置谓词:
// Dates
[request setPredicate:[NSPredicate predicateWithFormat:@"date == %@", datesEntity]];
或
// NSDate
[request setPredicate:[NSPredicate predicateWithFormat:@"date.date == %@", date]];
默认情况下,所有关系都将被提取为故障,如果您需要在此提取期间提取它们,您可以配置要在此提取中获取的关系键路径,如下所示:
NSArray* relationKeys = [NSArray arrayWithObject:@"disease"];
[request setRelationshipKeyPathsForPrefetching:relationKeys];
使用此方法,将获取所有相关的Disease
实体以及所有匹配的Dates
实体。