【问题标题】:avoid duplicate results on Core Data fetch避免 Core Data fetch 的重复结果
【发布时间】:2012-06-29 07:58:52
【问题描述】:

我有一个 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 *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];


}

如果我理解正确,设置 request.returnsDistinctResults=YES;应避免获取重复项。但是它不起作用,我看到此属性的值重复。

那里有我遗漏的东西吗?我会很感激那里的一些指示。提前谢谢你。

编辑:如果有人在这里遇到同样的问题,那么在应用 David 的回答后,生成的 fetchedResultsController 只是一个 NSDIctionary,其对象仅具有请求的值,仅用于显示目的就很好了。为了在单元格标签上显示结果,我在 cellForRowAtIndexPath 中所做的一件事是:

之前

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

之后

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

【问题讨论】:

    标签: xcode core-data nspredicate nsmanagedobject nsfetchrequest


    【解决方案1】:

    来自documentation of returnsDistinctResults

    仅当为propertiesToFetch 设置了值时才使用此值。

    来自documentation of propertiesToFetch

    仅当 resultType 设置为 NSDictionaryResultType 时才使用此值。

    来自documentation of resultType

    默认值为NSManagedObjectResultType


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

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

    request.resultType = NSDictionaryResultType;
    

    【讨论】:

    • 谢谢大卫!我不知道 fetch 上的这些选项,它们真的很有趣。谢谢!
    • 看来如果使用NSDictionaryResultType,那么就不需要设置returnDistinctResults了。如果没有设置returnDistinctResults,我不会得到重复。
    • @Jeff 这很奇怪,因为根据文档,默认值为NO
    【解决方案2】:

    除了 David Rönnqvist 的回答之外,我建议提供一个有用的链接(带有示例),以使用 Core Data 选择不同的值:

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

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      相关资源
      最近更新 更多