Ios CoreData:使用NSPredicate筛选一对多对多关系(此处不允许对多个键出错)

Ios CoreData:使用NSPredicate筛选一对多对多关系(此处不允许对多个键出错),ios,objective-c,core-data,nspredicate,Ios,Objective C,Core Data,Nspredicate,我有coreData模型: 我正在尝试验证电影的时间表是否与电影和剧院相匹配,但我得到了错误“这里不允许使用多个键” 这是我的密码: NSManagedObjectContext *moc = [self managedObjectContext]; NSEntityDescription *timeDescription = [ NSEntityDescription entityForName:@"Schedules" inManagedObjectContext:moc];

我有coreData模型:

我正在尝试验证电影的时间表是否与电影和剧院相匹配,但我得到了错误“这里不允许使用多个键”

这是我的密码:

NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *timeDescription = [ NSEntityDescription entityForName:@"Schedules" inManagedObjectContext:moc];
    NSPredicate *timePredicate  = [NSPredicate predicateWithFormat:@"showTimes <= %@", _movieTime];
    NSPredicate *moviePredicate = [NSPredicate predicateWithFormat:@"ANY movie.nameOfMovie CONTAINS[cd] %@", _movie];
    NSPredicate *theatersPredicate = [NSPredicate predicateWithFormat:@"movie.theaters.nameOfTheater == %@", _theaterName];
    NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[timePredicate,moviePredicate,theatersPredicate]];

    NSFetchRequest *request = [NSFetchRequest new];
    request.predicate = compoundPredicate;
    request.entity = timeDescription;
    NSError *error = nil;

    NSArray *result = [moc executeFetchRequest:request error:&error];
NSManagedObjectContext*moc=[self-managedObjectContext];
NSEntityDescription*timeDescription=[NSEntityDescription entityForName:@“Schedules”在托管对象上下文:moc中];
NSPredicate*timePredicate=[NSPredicate Predicate WithFormat:@“showTimes您得到的是“此处不允许使用多个键”错误,因为在您的
预测中,
“movie”是一个多对多关系。与
moviePredicate
相比,它工作正常,因为它使用了“ANY”关键字

因此,您可能会想在您的
剧院预告中添加“ANY”(任何)即可(即编译并运行正常),但我认为,它不会给出您想要的结果:它将显示那些(任何电影的moviename匹配项)和(任何电影的nameofatre匹配项)。但在每种情况下,它都可能是不同的电影

我认为您想要的是“任何电影(moviename匹配和NameOfaTre匹配)”。为此,请使用子查询:

NSPredicate *timePredicate  = [NSPredicate predicateWithFormat:@"showTimes <= %@", _movieTime];
NSPredicate *movieAndTheaterPredicate = [NSPredicate predicateWithFormat:@"SUBQUERY(movie, $x, $x.nameOfMovie CONTAINS[cd] %@ AND $x.theaters.nameOfTheater == %@).@count > 0", _movie, _theaterName];
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[timePredicate,movieAndTheaterPredicate]];
NSPredicate*timePredicate=[NSPredicate-predicateWithFormat:@“showTimes 0”,电影,终端名];
NSPredicate*compoundPredicate=[NSCompoundPredicate和PredicateWithSubPredicates:@[timePredicate,movieAndTheaterPredicate]];

为什么你不扩展我在你的另一个问题中用子查询给你的答案的谓词?如果它工作正常,只需添加一个进一步的AND条件和timeKind REPLICATE?既然你有相同的问题?@geo,我的问题是当我的实体是“调度”到“剧院”时如何返回“.NSPredicate*theatersPredicate=[NSPredicate predicateWithFormat:@”movie.theates.nameofater==%@“,_theaternem];。@Lame,这是完全不同的问题。rry,现在已删除了谓词中的印刷错误(多余的)。
NSPredicate *timePredicate  = [NSPredicate predicateWithFormat:@"showTimes <= %@", _movieTime];
NSPredicate *movieAndTheaterPredicate = [NSPredicate predicateWithFormat:@"SUBQUERY(movie, $x, $x.nameOfMovie CONTAINS[cd] %@ AND $x.theaters.nameOfTheater == %@).@count > 0", _movie, _theaterName];
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[timePredicate,movieAndTheaterPredicate]];