Coredata不支持谓词,但仅支持非家庭计算

时间:2012-01-25 17:05:01

标签: ios core-data nspredicate

我正在关注苹果关于有效导入数据的示例(http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/Articles/cdImporting.html

我的代码示例如下所示

NSString* predicateString = @" IN %@)";
predicateString = [NSString stringWithFormat:@"(%@%@", predicatePrefix, predicateString];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName:entityName inManagedObjectContext: self.managedObjectContext]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:predicateString, sortedServerIDs]];

// make sure the results are sorted as well
[fetchRequest setSortDescriptors: [NSArray arrayWithObject:
                                   [[NSSortDescriptor alloc] initWithKey: predicatePrefix
                                                               ascending:YES]]];

NSError *error = nil;
NSArray *matchingManagedObjects = [__managedObjectContext executeFetchRequest:fetchRequest error:&error];

产生的错误是

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: 'unimplemented SQL generation for predicate : (businessesID IN {1, 2, 3, 4, 5, 6, 7, 39})'

然而,这可以在我的家用电脑上在模拟器中使用,它可以在我的设备上运行! 我在两台机器上运行xcode 4.2.1。

我试着听下这个答案: Does CoreData on iPhone support IN predicates?

但收到同样的错误

如果在我的机器上检查我的SQLite版本时有问题,我有:

sqlite3 --version 3.7.8 2011-09-19 14:49:19 3e0da808d2f5b4d12046e05980ca04578f581177

1 个答案:

答案 0 :(得分:0)

当您引用字段名称时,您需要在格式字符串中使用%K

NSString* predicateString = @" IN %@)";
predicateString = [NSString stringWithFormat:@"(%K%@", predicatePrefix, predicateString];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName:entityName inManagedObjectContext: self.managedObjectContext]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:predicateString, sortedServerIDs]];

// make sure the results are sorted as well
[fetchRequest setSortDescriptors: [NSArray arrayWithObject:
                               [[NSSortDescriptor alloc] initWithKey: predicatePrefix
                                                           ascending:YES]]];

NSError *error = nil;
NSArray *matchingManagedObjects = [__managedObjectContext executeFetchRequest:fetchRequest error:&error];