0

我有一个循环数据的核心数据导入器,并在导入过程中忽略重复记录。

但是我发现我的 NSFetchRequest 与最近存储的尚未保存的记录不匹配。而且我看到看似相同的查询提供了不同且出乎意料的结果。

例如,在我的测试中,我发现这个查询匹配并返回结果:

fetchTest.predicate = [NSPredicate predicateWithFormat:@"%K = 3882", @"intEmployee_id"];

但这个看似相同的却没有:

fetchTest.predicate = [NSPredicate predicateWithFormat:@"%K = %@", @"intEmployee_id", @"3882"];

但是 - 在上下文保存到持久存储后,它们都匹配相同。

Apple 的文档说,默认情况下,fetches 应该对挂起的更改起作用,实际上我已经遵守了 [fetchTest includesPendingChanges] = YES。

有什么想法吗?这两个提取怎么可能返回不同的结果?

4

2 回答 2

1

也许员工 ID 不是字符串而是数字?那么谓词应该是:

[NSPredicate predicateWithFormat:@"%K = %@", @"intEmployee_id",
                                             [NSNumber numberWithInt:3882]];

这意味着不稳定的行为来自混合类型。它仍然以某种方式工作,即使不规律,因为在 SQLite 文档中它说 SQLite 在物理存储数据时实际上并没有真正区分类型。

请参阅SQLite 网站上的 SQLite 的独特功能,标题为Manifest Typing

于 2012-05-06T20:40:53.473 回答
1

这些实际上不会评估为相同的值。

[NSPredicate predicateWithFormat:@"%K = 3882", @"intEmployee_id"]

评估为intEmployee_id = 3882while

[NSPredicate predicateWithFormat:@"%K = %@", @"intEmployee_id", @"3882"]

评估为intEmployee_id = "3882"

尝试使用数字而不是字符串作为 id。

于 2012-05-06T20:44:07.830 回答