核心数据提示:没有这样的专栏

时间:2015-03-11 14:12:50

标签: ios objective-c core-data subquery nspredicate

NSFetcheRequest中使用谓词时出现问题。

我想让所有联系人至少有phone.internationalNumber == phoneNumberToCompareWith

这是我正在使用的谓词:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(contact.phones, $phone, $phone.internationalNumber == %@).@count != 0",phoneNumberToCompareWith];

我收到错误:

  

2015-03-11 14:54:17.041 appName [1370:390550]核心数据:错误:   -executeRequest:遇到异常=数据库的I / O错误       /var/mobile/Containers/Data/Application/F262488E-30BA-414E-979A-D316F53041C3/Documents/appName.sqlite。   SQLite错误代码:1,'没有这样的列:t2.ZINTERNATIONALNUMBER'用   userInfo = {               NSFilePath =“/ var / mobile / Containers / Data / Application / F262488E-30BA-1414-J-979A-D316F53041C3 / Files / appName.sqlite”;               NSSQLiteErrorDomain = 1;           }

实体关系: enter image description here

[编辑]:这个谓词做了三重奏:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%@ in contact.phones.internationalNumber)", phoneNumberToCompareWith];

但是因为我从不使用SUBQUERY可以告诉我如何使用SUBQUERY做同样的事情,谢谢

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式完成此操作:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY contact.phones.internationalNumber = %@", phoneNumberToCompareWith];

子查询等效于:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(contact.phones, $x, $x.internationalNumber = %@).@count!=0", phoneNumberToCompareWith];

所以你的子查询是正确的。我建议你手动打开sqlite文件并检查数据结构。

可能是路径:

/var/mobile/Containers/Data/Application/F262488E-30BA-414E-979A-D316F53041C3/Documents/appName.sqlite

之后打开sql debug到你的目标,看看核心数据究竟发送给sqlite。

产品 - >方案 - >编辑 添加“-com.apple.CoreData.SQLDebug 1”

尝试直接在db上运行sqlite3中有问题的sql。 最后,尝试重新创建数据库,但要小心数据。