avoid duplicate results on Core Data fetch

落爺英雄遲暮 提交于 2019-12-09 13:51:07

问题


I have a subclass of the CoreDataTableViewController (subclass of UITAbleViewController dome by the people on Stanford done to link CoreData and TableViews). On this Class, I want to perform a fecth, sorting by an attribute called "definition" and the code which executes it is the following:

- (void)setupFetchedResultsController{


    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:self.entity];

    request.propertiesToFetch=[NSArray arrayWithObject:@"definition"];
    request.returnsDistinctResults=YES;

    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"%K != nil", @"definition"]; 
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"%K != ''", @"definition"];
    NSPredicate *predicate3=  [NSPredicate predicateWithFormat:@"%K contains[cd] %@", @"definition", self.seachBar.text];

    NSArray *prepredicateArray;

    if ([self.seachBar.text length]) {
         prepredicateArray = [NSArray arrayWithObjects:predicate1, predicate2, predicate3,nil];

    }else {
         prepredicateArray = [NSArray arrayWithObjects:predicate1, predicate2,nil];

    }

    request.predicate=[NSCompoundPredicate andPredicateWithSubpredicates:prepredicateArray];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"definition" ascending:YES ]];





    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.managedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];




    [self performFetch];


}

If I understood it correctly, setting request.returnsDistinctResults=YES; should avoid fetching duplicates. However it doesn't work and I'm seeing duplicates of this attribute's value.

Is there something I'm missing there? I'd appreciate some pointings there. Thank you in advance.

EDIT: If anyone is having the same issue here, after applying David's answer the resulting fetchedResultsController is just a NSDIctionary with object with only the requested value, which for displaying only purposes is quite fine. One thing I've done in cellForRowAtIndexPath in order to display the results on the cell label is:

Before:

HNMR *hnmr = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text=hnmr.definition;

After:

cell.textLabel.text=[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:@"definition"];

回答1:


From the documentation of returnsDistinctResults:

This value is only used if a value has been set for propertiesToFetch.

From the documentation of propertiesToFetch:

This value is only used if resultType is set to NSDictionaryResultType.

From the documentation of resultType:

The default value is NSManagedObjectResultType.


This all tells me that the propertiesToFetch is ignored because you haven't set the resultType yourself and the default it to return managed objects instead of dictionaries. Since the propertiesToFetch is ignored the returnsDistinctResults is ignored as well and thus you are still getting duplicates.

Try setting the result type to return dictionaries instead of managed objects.

request.resultType = NSDictionaryResultType;



回答2:


In addition to David Rönnqvist answer I suggest a useful link (with a sample) on selecting distinct values with Core Data:

core-data-how-to-do-a-select-distinct

Hope that helps.



来源:https://stackoverflow.com/questions/11233789/avoid-duplicate-results-on-core-data-fetch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!