2016-03-30 7 views
0
私は2台を持っています

にいずれかの述語を追加する方法、今私は、製品固有のオプションを持っを取得したいと思います。コアデータ 製品と複数のオプションを持つことができます。オプション</p> <p>製品に多くの関係Core DataはiOSの

私はエラー(キャッチされない例外が原因***終了アプリ「NSInvalidArgumentException」、理由はこの

enter image description here

ための句はそのクラッシュ述語を書く方法は考えていません:「まで、多くのキーを私はこのためにコード

-(Product*)getProdcutFromDB{ 

AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate]; 
NSManagedObjectContext *context = appDelegate.managedObjectContext; 

NSError *error; 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *orderEntity = [NSEntityDescription 
            entityForName:@"Product" inManagedObjectContext:context]; 
[fetchRequest setEntity:orderEntity]; 
NSPredicate* predicate = [NSPredicate predicateWithFormat:@" options.optionID == '14'"]; 
[fetchRequest setPredicate:predicate]; 
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 

Product* orderEntityForProdcut=nil; 
if (fetchedObjects.count>0) { 
    orderEntityForProdcut=(Product*)[fetchedObjects objectAtIndex:0]; 
    NSLog(@"%@",orderEntityForProdcut.productName); 
    NSLog(@"%@",orderEntityForProdcut.options); 

    return orderEntityForProdcut; 
} 

return nil; 
} 

答えて

1

の下にしようとすると、ここでは使用できません」 )、あなたはSUBQUERY()を使用する必要があります。

(別の文字列はSOで読みやすいようです。)別に

NSString *predicateFormat = @"SUBQUERY(options, $o, $o.optionID == %@)[email protected] > 0"; // The "> 0" condition could also be "== 1", depending on your need. 

NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, optID]; 

それが文字列でハードコードのプロパティ名に最善ではありません。

// These would be defined somewhere that could be imported as needed. 
NSString *const kOptionsKey = @"options"; 
NSString *const kOptionIDKey = @"optionID"; 

NSString *predicateFormat = @"SUBQUERY(%K, $o, $o.%K == %@)[email protected] > 0"; 

NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, 
             kOptionsKey, kOptionsIDKey, optID]; 

プロパティ名を変更すると、更新する必要があるのは1つの定数だけという考えがあります。これはまた、文字列内のプロパティ名のタイプミスのより一般的な発生を防ぎます。あなたが答えるために

+0

おかげで、偉大なその作業は、あなたも私が追加できる方法を提案してください可能性があり、「私がしなければならない製品が必要な場合、私は意味し、ここで」ANDオプションIDが12,13および14の場合 –

+0

eを定義するこれらの述語を別々に処理し、次にNSCompoundPredicateを使用して結合します。 predicateFormat、 kOptionsKey、kOptionIDKey: NSPredicate * predicate2 = [NSPredicate predicateWithFormat;: – Avi

+0

あなたは私が述語に新しいですサンプル:( –

0

おかげ@Aviは、私の問題は 今すぐコードを更新解決された私はoptionId =「」とoptionIdを有する製品のリストを取得することができています。このコードの後に​​複数持つ述語AND条件

を使用してここにあります=「」というように.....

-(Product*)getProdcutFromDBNew{ 

AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate]; 
NSManagedObjectContext *context = appDelegate.managedObjectContext; 

NSError *error; 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *orderEntity = [NSEntityDescription 
            entityForName:@"Product" inManagedObjectContext:context]; 
[fetchRequest setEntity:orderEntity]; 

// These would be defined somewhere that could be imported as needed. 
NSString *const kOptionsKey = @"options"; 
NSString *const kOptionIDKey = @"optionID"; 

NSString *predicateFormat = @"SUBQUERY(%K, $o, $o.%K == %@)[email protected] > 0"; 


NSPredicate *predicate1 = [NSPredicate predicateWithFormat:predicateFormat, 
          kOptionsKey, kOptionIDKey, @"12"]; 
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:predicateFormat, 
          kOptionsKey, kOptionIDKey, @"14"]; 


// Add the predicates to the NSArray 

NSArray *subPredicates = [[NSArray alloc] initWithObjects:predicate1, predicate2, nil]; 

NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates]; 



// NSPredicate* predicate = [NSPredicate predicateWithFormat:@" ANY options.name == 'Size'"]; 
[fetchRequest setPredicate:compoundPredicate]; 

NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 

Product* orderEntityForProdcut=nil; 
if (fetchedObjects.count>0) { 
    orderEntityForProdcut=(Product*)[fetchedObjects objectAtIndex:0]; 
    NSLog(@"%@",orderEntityForProdcut.productName); 
    NSLog(@"%@",orderEntityForProdcut.options); 

    return orderEntityForProdcut; 
} 

return nil; 

}

関連する問題