【问题标题】:Core Data - detect equal properties in fetched objects核心数据 - 检测获取对象中的相等属性
【发布时间】:2014-02-13 08:46:14
【问题描述】:

我的核心数据模型:

Contact
=======

name
phone number (unique)

我需要显示具有以下条件的联系人的表格视图:

如果有多个联系人具有相同的姓名 - 显示姓名+号码
否则 - 仅显示名称

例如,如果我的核心数据联系人是:

Michael, 11112221  
Jon, 33438282  
Jon, 72727272  
Lisa, 99939393  

我的表格视图应该出现:

Jon (33438282)  
Jon (72727272) 
Lisa   
Michael  

目前我正在使用以下 NSFetchRequest 来显示联系人列表:

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Contact"];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]];
request.fetchBatchSize = 20;

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

我正在寻找最有效的方法来知道哪个名字出现了多次,这样我就可以在名字旁边显示数字。

【问题讨论】:

    标签: ios iphone objective-c core-data nsfetchedresultscontroller


    【解决方案1】:

    如果您使用 NSSQLiteStoreType 作为持久存储,您可以使用 NSExpression 获取重复项:

    NSEntityDescription *entity        = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
    NSAttributeDescription *nameDesc   = [entity.attributesByName objectForKey:@"name"];
    NSExpression *keyPathExpression    = [NSExpression expressionForKeyPath:@"name"];
    NSExpression *countExpression      = [NSExpression expressionForFunction:@"count:" arguments:@[keyPathExpression]];
    
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    [expressionDescription setName: @"count"];
    [expressionDescription setExpression: countExpression];
    [expressionDescription setExpressionResultType: NSInteger32AttributeType];
    
    NSError *error = nil;
    NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Contact"];
    [fetch setPropertiesToFetch:@[nameDesc, expressionDescription]];
    [fetch setPropertiesToGroupBy:@[nameDesc]];
    [fetch setResultType:NSDictionaryResultType];
    
    NSArray *results = [self.managedObjectContext executeFetchRequest:fetch error:&error];
    NSArray *duplicates = [results filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"count > 1"]]; 
    NSArray *duplicateNames = [duplicates valueForKeyPath:@"name"];
    

    duplicateNames 包含在数据库中多次出现的联系人姓名。因此,每当表格视图中显示联系人时,只需查询duplicateNames 数组即可检查实际联系人是否重复。

    【讨论】:

      【解决方案2】:

      最简单的方法,如果你只想修改显示我会修改tableView:cellForRowAt...来检查当前人的上方和下方的人的名字

      在基本模拟代码中:

      - tableView:cellForRowAtIndexPath:(id)path {
          ....
          Person *prior = ...
          Person *next = ...
      
          if(prior.name == current.name || next.name == current.name) {
              //show name+number
          }
      }
      

      【讨论】:

      • 感谢您的回复,但我不能使用这种方法。我没有在我的问题中提到它,但我有不止一个部分,这个其他部分也显示联系人但不是全部。所以如果用户没有在我的主要部分滚动到这个联系人,我就有问题
      • 我想我必须再做一次提取,我只是想知道什么是最有效的。
      • :D 是的,你没有提到这一点.. 但为此,我将向你展示最优雅的方式!
      • 实际上 vbalis 的方式看起来不错。每当您修改数据库时重新运行查询
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多