简体   繁体   English

核心数据,NSPredicate和to-many密钥

[英]Core Data, NSPredicate and to-many key

I have a Core Data model in which a Task entity includes an optional to-many relationship excludedOccurrences. 我有一个Core Data模型,其中Task实体包含一个可选的to-many关系excludedOccurrences。 One of the properties of excludedOccurrences is start, which is an NSDate object. excludedOccurrences的一个属性是start,它是一个NSDate对象。 The ExcludedOccurrence entity has an inverse mandatory to-one relationship to the Task entity. ExcludedOccurrence实体与Task实体具有反向强制一对一关系。

In order to fetch tasks for a specified day, I need to make sure that the specified day does not appear as the start property of any ExcludedOccurrence entity. 为了获取指定日期的任务,我需要确保指定的日期不会显示为任何ExcludedOccurrence实体的start属性。 One of the sub-predicates I am trying to use is therefore 因此,我试图使用的一个子谓词

NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"(ALL excludedOccurrences.start != %@))", today];

where today is a NSDate object for today including only the day, month and year components. 今天是今天的NSDate对象,仅包括日,月和年组件。 All of the excluded occurrences start properties also include just the day, month and year components. 所有排除的事件启动属性也仅包括日,月和年组件。

While this should fine, at least reading the documentation for Core Data and NSPredicate, I get the following error message: 虽然这应该没问题,至少阅读Core Data和NSPredicate的文档,但是我收到以下错误消息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'不支持的谓词

If I use the equivalent predicate 如果我使用等效谓词

NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"!(ANY excludedOccurrences.start == %@))", today];

no exception is thrown, however the code does not work as expected: the occurrence for today, which should not be excluded, is instead excluded. 没有抛出任何异常,但代码不能按预期工作:而是排除了今天不应排除的事件。

I am not sure also how to test for the case excludedOccurrences == nil: the following predicate 我不确定如何测试caseOccurrences == nil:以下谓词

NSPredicate *nilPredicate = [NSPredicate predicateWithFormat: @"(excludedOccurrences == nil)"];

causes at runtime the exception 在运行时导致异常

to-many key not allowed here 这里不允许使用多个密钥

However, since the excludedOccurrences relationship is optional, I also need to test if it is nil. 但是,由于excludedOccurrences关系是可选的,我还需要测试它是否为零。

How do I deal with this? 我该如何处理? Thank you in advance. 先感谢您。

To test for an empty relationship you should compare the count of the to-many key to zero. 要测试空关系,您应该将to-many键的计数与零进行比较。

[NSPredicate predicateWithFormat:@"excludedOccurrences.@count == 0"];

As for your subpredicates, be aware that you can only have one of either the ALL or ANY modifiers in your final predicate, although you can use that modifier multiple times throughout the predicate. 至于你的subpredicates,请注意你的最终谓词中只能有ALLANY修饰符之一,尽管你可以在整个谓词中多次使用该修饰符。

Not OK : ANY foo.bar = 1 AND ALL foo.baz = 2 不行ANY foo.bar = 1 AND ALL foo.baz = 2
OK: ANY foo.bar = 1 AND !(ANY foo.baz != 2) 好的: ANY foo.bar = 1 AND !(ANY foo.baz != 2)

with the help of you all, I finally managed to determine the correct predicate for my scenario. 在你们的帮助下,我终于设法为我的场景确定了正确的谓词。 It looks like that an NSDate object is handled as a double, however, the double is never something like 3.7, it is always like 3.0 Therefore, the following predicate correctly works in my tests: 看起来NSDate对象被处理为double,但是,double从不像3.7,它总是像3.0因此,以下谓词在我的测试中正确工作:

NSPredicate *occurrenceIsNotExcludedPredicate = [NSPredicate predicateWithFormat: @"(excludedOccurrences.@count == 0 || (excludedOccurrences.@count > 0 && NONE excludedOccurrences.start == %@))",thisDate];

where thisDate is a NSDate object containing only the day, month and year components (as in the case of the start property of the ExcludedOccurrence entity. 其中thisDate是仅包含日,月和年组件的NSDate对象(如ExcludedOccurrence实体的start属性的情况)。

Testing for an empty relationship is basically done using the @count aggregate operator, as suggested by some folks at Apple. 正如Apple的一些人所建议的那样,基本上使用@count聚合运算符来测试空关系。

Again, thankyou very much for your help. 再次,非常感谢你的帮助。 I still observe that the documentation is flawed in several parts (especially where it says that ALL works fine while, instead, it does not work at all). 我仍然观察到文档在几个部分存在缺陷(特别是在它说ALL工作正常的情况下,相反,它根本不起作用)。

So, to test for a non-empty relationship, this actually works: 因此,为了测试非空关系,这实际上是有效的:

[NSPredicate predicateWithFormat:@"relationship.@count != 0"]

The solution given by Ashley Clark crashes for me giving "to-many key not allowed here" 阿什利·克拉克给出的解决方案让我崩溃了“在这里不允许使用许多钥匙”

在swift 2中,类似于:

request.predicate = NSPredicate(format: " relationship.@count != 0")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM