2016-03-30 2 views
0

I 2 Tabelle Produkt- und Option in KerndatenWie Prädikat hinzufügen für eine zu vielen Beziehung Core Data iOS

Ein Produkt habe können mehrere Optionen haben, jetzt würde Ich mag Produkt mit einer spezifischen Option erhalten.

Ich habe keine Ahnung, wie Prädikat zu schreiben, wo Klausel für diese

enter image description here

Die Abstürze mit Fehler (*** Beenden app aufgrund nicht abgefangene Ausnahme 'NSInvalidArgumentException' Grund: ‚to-many-Schlüssel hier nicht erlaubt‘ ), wenn ich Code unten versuchen

-(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; 
} 

Antwort

1

Dazu müssen Sie SUBQUERY() verwenden.

(Die separate Zeichenfolge ist es einfach, SO lesen auf machen.)

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

Abgesehen: Es ist am besten nicht zu hart Code Eigenschaftsnamen in Strings. Es ist besser, die wie oben zu schreiben:

// 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]; 

Die Idee ist, dass, wenn der Name der Eigenschaft alle eine einzige Konstante ist, die benötigte Aktualisierung geändert wird. Dies verhindert auch das häufiger auftretende Auftreten eines Tippfehlers im Eigenschaftsnamen innerhalb einer Zeichenfolge.

+0

Dank, Groß seine Arbeits, könnten Sie auch bitte vorschlagen, wie kann ich hinzufügen „AND“ hier meine ich, wenn ich das Produkt benötigen die muss habe die Optionen ID 12,13 und 14 –

+0

Einfach e definieren ach dieser Prädikate separat und kombinieren sie dann mit 'NSCompoundPredicate'. – Avi

+0

Können Sie ein Beispiel: Ich bin Prädikat :( –

0

Dank @Avi für Sie zu beantworten, hat mein Problem nun gelöst Code aktualisiert werden hier mit mehreren Prädikaten mit AND Bedingung

Nach diesem Code ich in der Lage bin Liste der Artikel zu erhalten, die OptionID = „“ und OptionID = „“ und so weiter .....

-(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; 

}

Verwandte Themen