使用NSFetchedResultsController时是否可以获得明显的结果?

Sle*_*lee 6 iphone core-data objective-c nsfetchedresultscontroller

我有一个产品搜索,搜索我的产品所在的ProductCategories,有时我的产品有多个类别,这给我重复的结果.我不想直接搜索产品表,因为有几种产品有多种尺寸,但基本上是相同的产品.

有没有办法用NSFetchedResultsController获得不同的搜索结果?

Shi*_*goo 11

是的你可以...

寻找方法

- (NSFetchedResultsController *)fetchedResultsController;
Run Code Online (Sandbox Code Playgroud)

并添加以下行(在此示例中,我们只获得托管对象的不同"title"属性):

[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:@"title"]];
self.fetchedResultsController.delegate = nil;
Run Code Online (Sandbox Code Playgroud)

您必须注意如何从NSFetchedResultsController访问值...例如在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Run Code Online (Sandbox Code Playgroud)

使用以下代码访问数据:

NSDictionary* title = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [title objectForKey:@"title"];
Run Code Online (Sandbox Code Playgroud)


Ale*_*exR 8

除了这Shingoo提供的解决方案,请不要忘记设置NSFetchedResultsController的 委托零,以禁用自动更新,它不会与工作NSDictionaryResultType和独特的价值:

self.fetchedResultsController.delegate = nil; 
Run Code Online (Sandbox Code Playgroud)