2013-05-27 117 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