我有一个具有以下一对多关系的数据库: 设备 - 属性 - >属性
我想获得一个具有至少一个具有特定显示类型的属性的设备。
NSArray *energyDisplayTypes = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:18],[NSNumber numberWithInt:39],[NSNumber numberWithInt:50],[NSNumber numberWithInt:62],[NSNumber numberWithInt:63], nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Device"
inManagedObjectContext:context];
NSError *error;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"properties.displayType IN %@", energyDisplayTypes];
[fetchRequest setPredicate:predicate];
[fetchRequest setEntity:entity];
NSArray *products= [context executeFetchRequest:fetchRequest error:&error];
我一直得到这个例外:
'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : properties.displayType IN {18, 39, 50, 62, 63}'
我没有那么多的数据库经验,所以它很简单。如果有人能帮助我,我将不胜感激
答案 0 :(得分:3)
如果properties
是多对多关系,那么您需要指明要与您的数组匹配的属性的显示类型。有三种选择:any,all或none。我怀疑你想要任何的属性'显示要匹配的类型,因此您将使用:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY properties.displayType IN %@", energyDisplayTypes];