我花了几个小时试图让一个 fetch 工作。我需要删除重复项,所以我想我可以按照本指南core-data-how-to-do-a-select-distinct 但它总是给我一个空数组。
这是我的代码:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"RBTag" inManagedObjectContext:[self context]];
request.entity = entity;
request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"name"]];
request.returnsDistinctResults = YES;
request.resultType = NSDictionaryResultType;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSError *error = nil;
NSArray *distincResults = [[self context] executeFetchRequest:request error:&error];
NSLog(@"Result: %@", distincResults);
我没有错误。我检查了 NSPropertyDescription,它确实找到了我的财产。如果我注释掉:
request.resultType = NSDictionaryResultType;
然后我得到结果,但它并不明显。=(