【问题标题】:Custom Core Data SectionNameKeyPath自定义核心数据 SectionNameKeyPath
【发布时间】:2013-03-30 19:37:37
【问题描述】:

我是核心数据的新手,正在尝试弄清楚如何在我的 NSFetchedResultsController 中创建自定义 sectionNameKeyPath。我有一个名为acctPeriod 的属性的托管对象。这是一个NSString。我想根据该字段的前 4 个字符创建部分。前4个字符代表会计期间的年份,不需要保存。

我浏览了这个站点并看到了有关瞬态属性的帖子,但我似乎无法让它们发挥作用。基本上我想要这个然后为我的sectionNameKeyPath分配periodYear

@dynamic periodYear;

-(NSString *)periodYear
{
    return [self.acctPeriod substringToIndex:4];
}

任何帮助将不胜感激。

**更新: 使用 Martin R. answer,我能够让它按预期工作。

- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Billing" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"acctPeriod" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];

//Predicate
NSPredicate *pred = [NSPredicate predicateWithFormat:@"clients = %@", self.client];
NSLog(@"%@",pred);

//[fetchRequest setResultType:NSDictionaryResultType];
//[fetchRequest setReturnsDistinctResults:YES];

[fetchRequest setPredicate:pred];
[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"periodYear" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _fetchedResultsController;  
}

【问题讨论】:

    标签: ios core-data transient sections


    【解决方案1】:

    以下应该可以工作:实现periodYear 方法(将使用 作为托管对象子类的类扩展中的“部分名称键路径”):

    @interface Event (AdditionalMethods)
    - (NSString *)periodYear;
    @end
    
    @implementation Event (AdditionalMethods)
    - (NSString *)periodYear {
        return [self.acctPeriod substringToIndex:4];
    }
    @end
    

    确保将acctPeriod 用作获取请求的第一个(或唯一一个)排序描述符

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"acctPeriod" ascending:YES];
    NSArray *sortDescriptors = @[sortDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];
    

    periodYear 用作获取结果控制器的sectionNameKeyPath

    NSFetchedResultsController *_fetchedResultsController = [[NSFetchedResultsController alloc]
                      initWithFetchRequest:fetchRequest 
                       managedObjectContext:self.managedObjectContext 
                         sectionNameKeyPath:@"periodYear"
                                  cacheName:nil];
    _fetchedResultsController.delegate = self;
    self.fetchedResultsController = _fetchedResultsController;
    

    最后添加默认的titleForHeaderInSection方法:

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo name];
    }
    

    或者,您可以将periodYear 定义为托管对象的瞬态属性。 在这种情况下,它也不会存储在数据库中,但可以通过按需计算和缓存值的方式实现。

    Apple 开发者库中的 DateSectionTitles 示例项目演示了它是如何工作的。

    【讨论】:

    • 好的,效果很好。我之前尝试过这个,但问题是我只尝试加载我的 1 个实体 acctPeriod,并按 periodYear 对其进行排序。所以我使用 [fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:[entityProperties objectForKey:@"acctPeriod"],nil]];这似乎不起作用。我试图将 periodYear 作为 entityProperty 但我知道它不是。我收到此错误:“传递给 setPropertiesToFetch 的密钥路径 periodYear 无效:”有什么想法吗?
    • @theDVUSone:如果您使用propertiesToFetch,那么您可能必须将periodYear 定义为瞬态属性并将其添加到propertiesToFetch。我没有尝试过,但这是我的假设。
    • 我在声明一个实现 getter 的 @property 时犯了一个错误。这个解决方案或多或少指出它应该只是一个方法声明。
    【解决方案2】:

    我建议不要将瞬态属性用作sectionNameKeyPath,因为它会导致获取请求获得的所有对象出错,因此该属性可以用作部分路径。
    您最好定义 year 的持久属性并将其用作您的 sectionNameKeyPath
    像这样将您的 FRC sectionNameKeyPath 设置为 year

    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:self.managedObjectContext
                                          sectionNameKeyPath:@"year"
                                                   cacheName:nil/*your chioce*/];
    

    要将部分名称显示为表格中的标题,请实现:

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        id<NSFetchedResultsSectionInfo> sec = [self.fetchedResultsController sections][section];
        return [sec name];
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-26
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      相关资源
      最近更新 更多