core data fetch request does not return distinct results
我正在尝试从核心数据实体返回不同的结果,然后将其放入弹出菜单。我只希望具有请求的属性的实体一次出现在返回的字典中,以便可以将其用作另一个弹出窗口的谓词。
例如
记录:
名称| id
Test1 | 111
Test1 | 222
Test2 | 333
将返回
因此可以将其设置为
例如
使用
内容集为
的ID弹出窗口
因此,我根据在SO上阅读的文章开始了此获取请求。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | -(void)fetchItems { NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Equipment"]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Equipment" inManagedObjectContext:[[NSApp delegate] managedObjectContext]]; fetchRequest.entity = entity; fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"item"]]; fetchRequest.resultType = NSDictionaryResultType; [fetchRequest setReturnsDistinctResults:YES]; NSArray *dictionaries = [[[NSApp delegate] managedObjectContext] executeFetchRequest:fetchRequest error:nil]; NSLog (@"names: %@",dictionaries); } |
但是NSLog仍然返回Nondistinct记录!这是日志:
1 2 3 4 5 6 7 8 9 10 11 | names: ( { item = item2; }, { item = item1; }, { item = item1; } ) |
有人对为什么这种提取无法正常执行有任何想法吗?另外,根据我想做的解释,我在逻辑上是否正确?
谢谢
使用收集对象
在您的
1 | NSArray *dictionaries = [[[NSApp delegate] managedObjectContext] executeFetchRequest:fetchRequest error:nil]; |
添加此行:
1 | NSSet *setDictionaries = [NSSet setWithArray:dictionaries]; |
更改您的日志:
1 | NSLog (@"names: %@",setDictionaries); |
请参阅Apple文档。
关于收藏集也很有帮助