使用聚合操作时发生崩溃:“ALL”在 Core Data iOS 应用程序中
我正在开发一个 iPhone 应用程序,并且使用 Group 和 Contact 对象设置了一个简单的多对多关系。一个组可以有多个联系人,并且联系人可以属于多个组。
我正在尝试使用以下谓词选择特定联系人尚不属于的所有组。 (注意:uid 字段是我用来唯一标识联系人实体的字符串字段)
[NSPredicate predicateWithFormat:@"ALL contacts.uid != %@", contactUId]
根据 Apple 的谓词编程指南,ALL 聚合操作是有效的,但我收到以下异常,表明这是一个不受支持的谓词:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate (null)'
我可以使用使用此谓词选择联系人已属于的所有组的类似谓词,因此看起来我已正确定义了所有关系和字段。
[NSPredicate predicateWithFormat:@"ANY contacts.uid == %@", contactUId]
在构造谓词时抛出异常,而不是在我尝试实际执行获取请求时抛出异常,因此它似乎与我正在使用的语法相关,而不是与核心数据支持相关。我做错了什么?
I'm working on an iphone application and I have a simple many-to-many relationship set up with Group and Contact objects. A group can have many contacts and contacts can belong to multiple groups.
I'm trying to select all groups that a particular contact does NOT already belong to using the following predicate. (Note: the uid field is a string field that I used to uniquely identify contact entities)
[NSPredicate predicateWithFormat:@"ALL contacts.uid != %@", contactUId]
According to Apple's Predicate Programming Guide, the ALL aggregate operation is valid but I get the following exception indicating that this is an unsupported predicate:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate (null)'
I can use a similar predicate to select all groups that a contact does already belong to using this predicate so it appears that I have all of the relationships and fields defined properly.
[NSPredicate predicateWithFormat:@"ANY contacts.uid == %@", contactUId]
The exception is thrown when constructing the predicate and not when I'm trying to actually execute the fetch request so it seems to be related to the syntax I'm using rather than Core Data support. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
核心数据编程指南说
它继续描述了 NSPredicate 与 NSSQLiteStoreType 一起使用的一些其他限制,但这里使用的“ALL”是一个(未记录的)限制,与获取请求如何发出 SQL 有关。
在底层,CoreData 为您的架构生成三个表:
因此,当您调用 myGroup.contacts 时,会运行类似这样的操作:
一个点字符后面发生了很多事情!
无论如何,要真正满足您的查询,您需要这样的东西。我在一个实际的 SQLite CD 数据库上测试了这个,所以表名看起来很奇怪,但它仍然应该是可以理解的:
我不是 SQL 专家,但我的观察首先是这个查询会很慢,其次是所有这些都与我们开始时的 NSPredicate 相去甚远。因此,只有通过大量的努力,CD 才能针对您想要执行的操作提供 SQL 查询,并且它所提供的查询并不比 ObjC 中的简单实现好多少。
不管它的价值如何,苹果开发人员此处表示SQLite 不支持 ALL,相反的文档是错误的。不过该文档在 2013 年仍然存在,因此似乎没有人对此采取任何行动。
无论如何,您实际应该做的是这样的:
这将评估软件中的谓词。
The Core Data Programming Guide says
It goes on to describe some other limitations on the use of NSPredicate with NSSQLiteStoreType, but your use of "ALL" here is an (undocumented) limitation that has to do with how the fetch request emits SQL.
Under the hood, CoreData generates three tables for your schema:
And so when you call myGroup.contacts, something like this gets run:
There's a lot going on behind one dot character!
Anyway, to actually fulfill your query, you'd need something like this. I tested this on an actual SQLite CD database, so the table names look strange, but it should still be comprehensible:
I'm no SQL expert, but my observations are first of all that this query is going to be slow, and second of all that it is kind of a long ways from the NSPredicate that we started with. So it would be only through a great deal of effort that CD could up with an SQL query for what you want to do, and the query that it would come up with would not be much better than a naive implementation in ObjC.
For whatever it's worth, an Apple developer says here that ALL is unsupported in SQLite and the documentation to the contrary is wrong. That documentation is still there in 2013 though, so nobody seems to have done anything about it.
Anyway, what you should actually do is something like this:
This will evaluate the predicate in software.
基本语法没问题。这在我的测试中编译并运行:
很可能问题出在
contactUid
变量上。如果它没有值或者没有干净地转换为 NSPredicate 理解的字符串,那么它将导致崩溃。例如...导致您所描述的崩溃。
我会在将 contactUid 分配给谓词之前立即记录它,以查看其实际的字符串可转换值是什么。它在 NSLog 中的显示方式就是它在谓词中的显示方式,因为两者都使用相同的字符串格式。
The basic syntax is fine. This compiles and runs in my test:
Most likely the problem is with the
contactUid
variable. If it has no value or a value that does not cleanly convert to a string that NSPredicate understands, then it will cause the crash. E.g.... causes exactly the crash you describe.
I would log
contactUid
immediately before assigning it to the predicate to see what its actual, string convertible value is. The way it displays in NSLog is the way it will appear in the predicate because both use the same string formatting.根据记录,上面的警告来自 (2013) NSExpression 类参考,位于 与 Aggregate 相关的段落表达式。事实上,当某些运算符可在 SQL 中进行翻译时,它们是受支持的(ANY、NONE)。
For the record, the warning above is from the (2013) NSExpression Class Reference, in the paragraph relative to Aggregate Expressions. In fact some operators are supported (ANY, NONE) when they are translatable in SQL.