2013-05-27 7 views
12

を使用して対多の関係をフィルタリングI持っている私のコアデータモデルでは、次の2つのエンティティ:コアデータ - 述語

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

I:

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

私が持っている設定一対多の関係検索文字列に一致する製造元に属するすべての商品を返す述語を作成しようとしています。例えば。 2つのメーカー、 "King Nut"、 "Queen Nut"がある場合、 "Nut"の検索では、King NutとQueen Nutの両方で製作された製品がすべて返されます。

私の述語は、フィルタがProductエンティティにあるときは完全に機能しますが、Manufacturerエンティティでフィルタリングするときには、どの述語も機能しません。結果セットは空です。

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

何を意味するのですか? 'Product'sを取得しようとすると、リクエストエンティティは' Product'でなければなりません –

+0

はい。私のエンティティリクエストはProduct: –

答えて

4

は、Appleからthis文書を見てみましょう。これは、 "ANY"述部を使って何をしようとしているかの例です。 「ナット」あなたの要求は次のようになりますを含む製品名でManufacturer秒を取得するには

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

:「ナット」を含む名前のメーカー製Product Sを取得するには

+0

です。そのドキュメントには、例があります: "Matt * '"];のようなANY employees.firstNameと%@ "、wildcardedString];のようなANY employees.firstName。最初の例では一重引用符があり、二番目には引用符がありません。一重引用符を使用するかどうかを指定します(wildcardStringには一重引用符は含まれません) –

+0

wildcardedString = @ "Matt *" – Rambatino

+1

文書が期限切れになっていると仮定しています... –

18

、あなたの要求は次のようになります。

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演算子では使用できないため、contains演算子は使用できません(たとえば、ANY employees.firstNameには 'Matthew'が含まれています)。 – jose920405