使用聚合操作时发生崩溃:“ALL”在 Core Data iOS 应用程序中

发布于 2024-11-05 15:49:37 字数 690 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不知在何时 2024-11-12 15:49:37

核心数据编程指南说

获取和存储类型之间存在一些交互。在 XML、二进制和内存存储中,谓词和排序描述符的评估是在 Objective-C 中执行的,可以访问所有 Cocoa 的功能,包括 NSString 上的比较方法。另一方面,SQL 存储将谓词和排序描述符编译为 SQL,并在数据库本身中评估结果。

它继续描述了 NSPredicate 与 NSSQLiteStoreType 一起使用的一些其他限制,但这里使用的“ALL”是一个(未记录的)限制,与获取请求如何发出 SQL 有关。

在底层,CoreData 为您的架构生成三个表:

  • 一个用于联系的表
  • 一个用于组的表
  • 一个将它们关联起来的连接表

因此,当您调用 myGroup.contacts 时,会运行类似这样的操作:

select * from Group join JOIN_TABLE on Group.pk == JOIN_TABLE.group_pk join Contact on JOIN_TABLE.contact_pk == Contact.pk where Group.pk == 12

一个点字符后面发生了很多事情!

无论如何,要真正满足您的查询,您需要这样的东西。我在一个实际的 SQLite CD 数据库上测试了这个,所以表名看起来很奇怪,但它仍然应该是可以理解的:

select ZGROUP.Z_PK as outer_pk from ZGROUP where "myUID" not in 
(select ZCONTACT.ZUID as contact_uid from ZGROUP join Z_1GROUPS on Z_1GROUPS.Z_2GROUPS == ZGROUP.Z_PK join ZCONTACT on Z_1GROUPS.Z_1CONTACTS == ZCONTACT.Z_PK where ZGROUP.Z_PK == outer_pk)

我不是 SQL 专家,但我的观察首先是这个查询会很慢,其次是所有这些都与我们开始时的 NSPredicate 相去甚远。因此,只有通过大量的努力,CD 才能针对您想要执行的操作提供 SQL 查询,并且它所提供的查询并不比 ObjC 中的简单实现好多少。

不管它的价值如何,苹果开发人员此处表示SQLite 不支持 ALL,相反的文档是错误的。不过该文档在 2013 年仍然存在,因此似乎没有人对此采取任何行动。

无论如何,您实际应该做的是这样的:

NSFetchRequest *fetchRequest = ...
NSArray *result = [moc executeFetchRequest:fetchRequest error:&err];
result = [result filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ALL contacts.uid != %@", contactUId]];

这将评估软件中的谓词。

The Core Data Programming Guide says

There are some interactions between fetching and the type of store. In the XML, binary, and in-memory stores, evaluation of the predicate and sort descriptors is performed in Objective-C with access to all Cocoa's functionality, including the comparison methods on NSString. The SQL store, on the other hand, compiles the predicate and sort descriptors to SQL and evaluates the result in the database itself.

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:

  • a table for Contact
  • a table for Group
  • a join table that relates them

And so when you call myGroup.contacts, something like this gets run:

select * from Group join JOIN_TABLE on Group.pk == JOIN_TABLE.group_pk join Contact on JOIN_TABLE.contact_pk == Contact.pk where Group.pk == 12

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:

select ZGROUP.Z_PK as outer_pk from ZGROUP where "myUID" not in 
(select ZCONTACT.ZUID as contact_uid from ZGROUP join Z_1GROUPS on Z_1GROUPS.Z_2GROUPS == ZGROUP.Z_PK join ZCONTACT on Z_1GROUPS.Z_1CONTACTS == ZCONTACT.Z_PK where ZGROUP.Z_PK == outer_pk)

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:

NSFetchRequest *fetchRequest = ...
NSArray *result = [moc executeFetchRequest:fetchRequest error:&err];
result = [result filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ALL contacts.uid != %@", contactUId]];

This will evaluate the predicate in software.

庆幸我还是我 2024-11-12 15:49:37

基本语法没问题。这在我的测试中编译并运行:

NSString *contactUId=@"steve";
NSPredicate *p=[NSPredicate predicateWithFormat:@"ALL contacts.uid != %@", contactUId];
NSLog(@"p = %@",p);

//=> p = ALL contacts.uid != "steve"

很可能问题出在 contactUid 变量上。如果它没有值或者没有干净地转换为 NSPredicate 理解的字符串,那么它将导致崩溃。例如

NSString *contactUId;
NSPredicate *p=[NSPredicate predicateWithFormat:@"ALL contacts.uid != %@", contactUId];
NSLog(@"p = %@",p);

...导致您所描述的崩溃。

我会在将 contactUid 分配给谓词之前立即记录它,以查看其实际的字符串可转换值是什么。它在 NSLog 中的显示方式就是它在谓词中的显示方式,因为两者都使用相同的字符串格式。

The basic syntax is fine. This compiles and runs in my test:

NSString *contactUId=@"steve";
NSPredicate *p=[NSPredicate predicateWithFormat:@"ALL contacts.uid != %@", contactUId];
NSLog(@"p = %@",p);

//=> p = ALL contacts.uid != "steve"

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.

NSString *contactUId;
NSPredicate *p=[NSPredicate predicateWithFormat:@"ALL contacts.uid != %@", contactUId];
NSLog(@"p = %@",p);

... 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.

秋风の叶未落 2024-11-12 15:49:37

Core Data 不支持聚合表达式。

根据记录,上面的警告来自 (2013) NSExpression 类参考,位于 与 Aggregate 相关的段落表达式。事实上,当某些运算符可在 SQL 中进行翻译时,它们是受支持的(ANY、NONE)。

Aggregate expressions are not supported by Core Data.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文