避免在Core Data fetch上出现重复的结果 [英] avoid duplicate results on Core Data fetch

查看:343
本文介绍了避免在Core Data fetch上出现重复的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CoreDataTableViewController的子类(UITAbleViewController的子类,由斯坦福大学的人做的链接CoreData和TableViews)。在这个类,我想执行一个fecth,通过一个属性称为定义排序,执行它的代码如下:

   - (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 * prepredictArray;

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

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

}

request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:prepredictArray];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@definitionascending:YES]];





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




[self performFetch];


}

如果我正确理解, .returnsDistinctResults = YES;应避免提取重复。但是它不工作,我看到这个属性的值的重复。



有没有什么我缺少?我会感谢一些点。提前感谢。



EDIT :如果任何人在这里遇到相同的问题,在应用David的回答后,得到的fetchedResultsController只是一个NSDIctionary对象与只有请求的值,其仅用于显示的目的是相当精细。为了在单元格标签上显示结果,我在cellForRowAtIndexPath中所做的一件事是:



之前

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



之后:

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

解决方案

returnsDistinctResults的文档


此值仅适用于 propertiesToFetch


propertiesToFetch文档


此值仅适用于 resultType 设置为 NSDictionaryResultType




< /developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html#//apple_ref/occ/instm/NSFetchRequest/resultType\"> resultType的文档


默认值为 NSManagedObjectResultType







这一切告诉我, propertiesToFetch 被忽略,因为您没有自己设置 resultType ,并且默认它返回被管理对象而不是字典。因为 propertiesToFetch 被忽略,所以 returnsDistinctResults 也被忽略,因此你仍然得到重复。



尝试将结果类型设置为返回字典而不是托管对象。

  request.resultType = NSDictionaryResultType; 


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"];

解决方案

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;

这篇关于避免在Core Data fetch上出现重复的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆