Xcode 8.1 核心数据抓取具体属性

Xcode 8.1 core data fetching specific properties

我正在尝试使用以下代码从核心数据中获取特定的 属性:

NSFetchRequest *test = [[NSFetchRequest alloc] init];
test.entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:self.currentMainContext];
test.predicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
test.resultType = NSDictionaryResultType;
test.returnsDistinctResults = YES;
test.propertiesToFetch = @[@"property"];

NSArray *results = [self.currentMainContext executeFetchRequest:test error:error];

但我得到一个空数组。

如果我注释掉 test.resultType = NSDictionaryResultType;,那么我会按预期取回数据库中所有实体的数组。 NSDictionaryResultType 有什么问题?

resultTypeNSDictionaryResultType 的获取请求有一个特点。
Documentation 对于 includesPendingChanges 表示:

If the value is NO, the fetch request doesn't check unsaved changes and only returns objects that matched the predicate in the persistent store.

A value of YES is not supported in conjunction with the result type NSDictionaryResultType, including calculation of aggregate results (such as max and min). For dictionaries, the array returned from the fetch reflects the current state in the persistent store, and does not take into account any pending changes, insertions, or deletions in the context.

因此请确保您已保存更改,或使用 NSManagedObjectResultType