本文介绍了核心数据,NSPredicate和多对一关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据模型,其中任务实体包括可选的对多关系excludedOccurrences。 excludedOccurrences的一个属性是start,它是一个NSDate对象。 ExcludedOccurrence实体与任务实体具有逆强制的一对一关系。

I have a Core Data model in which a Task entity includes an optional to-many relationship excludedOccurrences. One of the properties of excludedOccurrences is start, which is an NSDate object. The ExcludedOccurrence entity has an inverse mandatory to-one relationship to the Task entity.

为了获取指定日期的任务,我需要确保指定的日期不会显示为任何ExcludedOccurrence实体的start属性。因此我尝试使用的子谓词之一是

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. One of the sub-predicates I am trying to use is therefore

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

今天是今天的NSDate对象,只包括日期,月份和年份。所有排除的出现开始属性也只包括日,月和年组件。

where today is a NSDate object for today including only the day, month and year components. All of the excluded occurrences start properties also include just the day, month and year components.

虽然这应该很好,至少阅读Core Data和NSPredicate的文档,我得到以下错误信息:

While this should fine, at least reading the documentation for Core Data and NSPredicate, I get the following error message:

由于未捕获的异常NSInvalidArgumentException而终止应用程序,原因:不支持的谓词

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported 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.

我不确定如何测试caseOxcurrency == nil:下面的谓词

I am not sure also how to test for the case excludedOccurrences == nil: the following predicate

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

会在运行时导致异常

-many key not allowed here

to-many key not allowed here

但是,由于excludedOccurrences关系是可选的,我还需要测试它是否为nil。

However, since the excludedOccurrences relationship is optional, I also need to test if it is nil.

如何处理这个问题?提前感谢。

How do I deal with this? Thank you in advance.

推荐答案

在所有的帮助下,我终于成功地为我的场景确定了正确的谓词。
看起来像一个NSDate对象被处理为double,但是,double永远不是3.7,它总是像3.0
因此,下面的谓词在我的测试中正确工作:

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.0Therefore, the following predicate correctly works in my tests:

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

其中thisDate是一个NSDate对象,仅包含日,月和年组件ExcludedOccurrence实体的开始属性。

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.

根据Apple的一些人的建议,测试空关系基本上是使用@count聚合运算符。

Testing for an empty relationship is basically done using the @count aggregate operator, as suggested by some folks at Apple.

同样,非常感谢您的帮助。
我仍​​然观察到文档在几个部分有缺陷(特别是它说,所有的工作正常,而不是不工作)。

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

这篇关于核心数据,NSPredicate和多对一关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:04