Is it possible to get great results when using NSFetchedResultsController?

I have a search for a product that searches the ProductCategories where my products are located, sometimes my products are in several categories, which gives me duplicate results. I do not want to look for the product table directly, because there are several products that have several sizes, but basically are the same product.

Is there a way to get various search results using NSFetchedResultsController?

+6
source share
4 answers

Yes, you can...

pay attention to the method

- (NSFetchedResultsController *)fetchedResultsController; 

and add the following lines there (in this example we get only a separate "title" attribute of our managed objects):

 [fetchRequest setReturnsDistinctResults:YES]; [fetchRequest setResultType:NSDictionaryResultType]; [fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:@"title"]]; self.fetchedResultsController.delegate = nil; 

you need to take care of how you access the values ​​from NSFetchedResultsController ... For example, in

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

use the following code to access the data:

 NSDictionary* title = [self.fetchedResultsController objectAtIndexPath:indexPath]; cell.textLabel.text = [title objectForKey:@"title"]; 
+11
source

In addition to the solution that Shingoo provided, be sure to set the NSFetchedResultsController delegate to nil to disable automatic updates that will not work with NSDictionaryResultType and various values:

 self.fetchedResultsController.delegate = nil; 
+8
source

You will need to use NSPredicate (see Predicate Programming Guide )

It is complicated, but you can do it using SUBQUERY

0
source

When creating NSFetchRequest you can use -setReturnsDistinctResults: and set it to YES .

-1
source

Source: https://habr.com/ru/post/894502/


All Articles