核心数据 – 使用谓词过滤多对多关系

我的核心数据模型中有以下两个实体:

Manufacture {name, ...other attributes} Product {name, .... other attributes} 

我建立了一对多的关系:

 Manufacturer.manufactures > Product.manufacturedBy 

我正在尝试构建一个谓词,以返回属于制造商的所有匹配搜索字符串的产品。 例如,如果有两个制造商,“King Nut”和“Queen Nut”,那么搜索“Nut”应该返回King Nut和Queen Nut制作的所有产品。

当我的filter在Product实体上时,我的谓词工作正常,但是在过滤制造商实体时,我无法获得任何谓词。 结果集为空。

 NSEntityDescription *entity = [NSEntityDescription entityForName:@"Product" inManagedObjectContext:[GBKDB context]]; searchValue = @"nut"; NSString *wildcardString = [NSString stringWithFormat:@"*%@*", searchValue]; 

我尝试过以下方法:

  predicate = [NSPredicate predicateWithFormat:@"manufacturedBy.name CONTAINS[cd] %@",searchValue]; predicate = [NSPredicate predicateWithFormat:@"manufacturedBy.name like %@",wildcardString]; predicate = [NSPredicate predicateWithFormat:@"manufacturedBy.name matches %@",wildcardString]; predicate = [NSPredicate predicateWithFormat:@"ALL manufacturedBy.name like %@",wildcardString]; predicate = [NSPredicate predicateWithFormat:@"ALL manufacturedBy.name like[cd] %@",@wildcardString]; 

要获得由名称中包含“nut”的制造商生产的Product ,您的请求应如下所示:

 NSString* searchVal = @"nut"; NSFetchRequest* r = [[NSFetchRequest alloc] initWithEntityName:@"Product"]; [r setPredicate:[NSPredicate predicateWithFormat:@"manufacturedBy.name CONTAINS[cd] %@",searchVal]]; 

要使Manufacturer的产品名称包含“坚果”,您的请求应如下所示:

 NSString* searchVal = @"nut"; NSFetchRequest* r = [[NSFetchRequest alloc] initWithEntityName:@"Manufacture"]; [r setPredicate:[NSPredicate predicateWithFormat:@"ANY manufactures.name CONTAINS[cd] %@",searchVal]]; 

如果结果集为空,则可能是因为没有对象回答谓词(包含子串“nut”)。
尝试添加一些具有已知名称和测试的假实体。

编辑:这是您可以用于测试的代码:

 typedef void(^config_block_t)(id); - (void) synthesizeObjectsOfEntity:(NSString*)entity context:(NSManagedObjectContext*)context count:(NSUInteger)count configBlock:(config_block_t)configBlock { for (;count;--count) { NSManagedObject* object = [NSEntityDescription insertNewObjectForEntityForName:entity inManagedObjectContext:context]; configBlock(object); } } - (void) synthesizeProductsAndManufacturersInContext:(NSManagedObjectContext*)context { NSMutableArray* manufacturers = [NSMutableArray new]; [self synthesizeObjectsOfEntity:@"Manufactur" context:context count:10 configBlock:^(Manufactur* m) { m.name = [NSString stringWithFormat:@"m-%u%u%u",arc4random()%10,arc4random()%10,arc4random()%10]; [manufacturers addObject:m]; }]; [self synthesizeObjectsOfEntity:@"Product" context:context count:100 configBlock:^(Product* p) { p.name = [NSString stringWithFormat:@"p-%u%u%u",arc4random()%10,arc4random()%10,arc4random()%10]; p.manufacturedBy = manufacturers[arc4random() % [manufacturers count]]; }]; [context save:NULL]; [context reset]; NSString* searchVal = @"3"; NSFetchRequest* r = [[NSFetchRequest alloc] initWithEntityName:@"Product"]; [r setPredicate:[NSPredicate predicateWithFormat:@"manufacturedBy.name CONTAINS[cd] %@",searchVal]]; NSArray* match = [context executeFetchRequest:r error:NULL]; NSLog(@"matched: %u",[match count]); } 

看看Apple提供的这份文件。 它做了一个例子来做你试图用“任何”谓词做的事情。