2013-05-27 119 views
12

一个一对多的关系,我有以下两个实体在我的核心数据模型:核心数据 - 过滤使用谓词

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

我已经建立了一对多的关系:

Manufacturer.manufactures <------>> Product.manufacturedBy 

我我试图构建一个谓词来返回属于制造商的与搜索字符串匹配的所有产品。例如。如果有两个制造商,“国王螺母”和“女王坚果”,则搜索“坚果”应该返回由国王螺母和皇后螺母制造的所有产品。

当我的过滤器位于产品实体上时,我的谓词完美地工作,但是当在制造商实体上过滤时,我无法获取任何谓词。结果集是空的。

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]; 
+0

你说的''“对制造商的实体过滤时”是什么意思?如果您尝试提取'产品',则您的请求实体必须为'产品' –

+0

是的。我的实体请求用于产品: –

回答

4

this文件看看苹果。它做了一个例子,你正在试图用“ANY”谓词来做什么。

+0

在该文档中,有以下示例:''ANY employees.firstName like'Matt *'“];'和'@”ANY employees.firstName like%@“,wildcardedString];'。在第一个例子中有单引号,第二个例子中没有。我应该使用单引号还是不使用单引号(wildcardString不包含单引号) –

+0

它们是完全一样的东西...假设wildcardedString = @“Matt *” – Rambatino

+1

文档已过期.. –

18

为了得到由制造商包含“坚果”的名字制造Product S,你的要求应该是这样的:

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

与含“螺母”你的要求应该像产品名获得Manufacturer S:

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

如果结果集是空的,这可能是由于没有物体回答谓词(包含子“螺母”)。
尝试添加一些具有已知名称和测试的假实体。

编辑: 这是代码,你可以使用测试:

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]); 
} 
+0

我想用你的第一个例子来返回产品...仍然不起作用!我的数据库不是空的,我有DBVisualizer可以查看数据。 –

+0

你确定你正确设置了'Manufactur'的'name'属性吗(它是可变形的,你有没有覆盖它)? –

+0

注意:您不能使用contains操作符(例如,ANY employees.firstName包含'Matthew'),因为contains操作符不能与ANY操作符一起使用。 – jose920405