【问题标题】:Is it possible to use a sectionKeyPath with NSFetchedResultsController when fetching properties with NSDictionaryResultType?使用 NSDictionaryResultType 获取属性时,是否可以使用带有 NSFetchedResultsController 的 sectionKeyPath?
【发布时间】:2014-11-14 19:51:41
【问题描述】:

我设置了一个获取请求来检索“用户”项目的一些属性:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:companyName, firstName, lastName, completeItems, incompleteItems, objectID, nil]];
[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:SortOptionCompany ascending:YES],
                                   [NSSortDescriptor sortDescriptorWithKey:SortOptionFirstName ascending:YES]]];
[fetchRequest setFetchBatchSize:kBatchSize];

我初始化我的 NSFetchedResultsController:

NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                      managedObjectContext:self.context
                                                                        sectionNameKeyPath:@"sectionIdentifier"
                                                                                 cacheName:nil];

属性“sectionIdentifier”是用户实体的临时属性。但是,当我执行此操作时,我收到消息:

"returned nil value for section name key path 'sectionIdentifier'. Object will be placed in unnamed section"

提取的信息未按需要分组。这是因为我需要获取整个 NSManagedObjects 以使 keyPath 不为零,还是有办法在检索字典时使用 sectionKeyPath?我也尝试过使用 setPropertiesToGroupBy: 没有成功。

【问题讨论】:

  • 您是否尝试在要获取的属性数组中包含 sectionIdentifier?
  • @pbasdf 是的,它不起作用,我猜是因为 sectionIdentifier 是一个瞬态属性。我尝试使用 propertiesToFetch: 和 sectionKeyPath: 中的非瞬态属性,结果与问题中显示的消息相同。
  • 经过一些测试,我已经能够让 sectionNameKeyPath 工作,但仅限于非瞬态属性。所以这是可能的。但是,如果我使用具有 nil 值的属性,我会收到相同的消息。
  • @pbasdf 如何确保该属性没有 nil 值?当我在 UITableViewCell 文本中显示它时,我试图用于 sectionKeyPath 的非瞬态属性不是 nil。
  • 您是否将其包含在您的propertiesToFetch 中?

标签: ios core-data mobile nsfetchedresultscontroller nsmanagedobject


【解决方案1】:

由于没有找到解决此问题的方法,因此我不使用 sectionKeyPath 并在检索后手动将数据分类到各个部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2011-09-06
    • 2011-03-15
    • 2020-12-14
    • 2020-07-14
    • 2020-08-12
    • 1970-01-01
    相关资源
    最近更新 更多