核心数据、NSPredicate 和对多键 [英] Core Data, NSPredicate and to-many key

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

问题描述

我有一个核心数据模型,其中一个任务实体包含一个可选的多对多关系 excludeOccurrences.excludeOccurrences 的属性之一是 start,它是一个 NSDate 对象.ExcludedOccurrence 实体与 Task 实体具有反向强制一对一关系.

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 实体的开始属性出现.因此,我尝试使用的子谓词之一是

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.

我也不确定如何测试excludedOccurrences == nil:以下谓词

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

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

在运行时导致异常

此处不允许多对多键

但是,由于 excludeOccurrences 关系是可选的,所以我还需要测试它是否为 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 对象被处理为双精度值,但是,双精度值永远不会像 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.0 Therefore, 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 实体的 start 属性的情况.

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.

再次感谢您的帮助.我仍然观察到文档在几个部分存在缺陷(特别是它说 ALL 工作正常,而它根本不起作用).

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 和对多键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆