在 Coredata 中,如何获取特定 table 的所有 ID?

In Coredata, how to get all IDs for specific table?

我想从 coredata table 中获取所有 ID。例如,有一个名为 Person 的 Table。有一个personId来唯一标识一条记录。我想获取所有留在 Person table 中的 personId。我不知道如何查询。

在 SQLite 管理器中,我可以通过执行以下查询来完成:

SELECT ZPERSONID FROM ZPERSON;

但我不知道如何为 iOS Coredata 做。

希望这会有所帮助:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
request.resultType = NSDictionaryResultType;
request.returnsDistinctResults = YES;
request.propertiesToFetch = @[@"personID"];
NSError *error;
NSArray *persons = [yourManagedContext executeFetchRequest:request error:&error];