自定义Core数据SectionNameKeyPath [英] Custom Core Data SectionNameKeyPath

查看:173
本文介绍了自定义Core数据SectionNameKeyPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的核心数据,我试图找出如何创建自定义 sectionNameKeyPath 在我的 NSFetchedResultsController 。我有一个具有属性 acctPeriod 的managed对象。它是一个 NSString 。我想根据此字段的前4个字符创建节。前4个字符表示会计期间的年份,不需要保存。



我已经浏览了这个网站,看到了关于临时属性的帖子,但我似乎不能让他们工作。基本上我想要这个,然后为 sectionNameKeyPath 分配 periodYear

  @dynamic periodYear; 

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

任何帮助将不胜感激。



**更新:
使用Martin R.答案,我能够按预期工作。

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

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

//适当地编辑实体名称。
NSEntityDescription * entity = [NSEntityDescription entityForName:@BillinginManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

//将批量大小设置为合适的数量。
[fetchRequest setFetchBatchSize:20];

//根据需要编辑排序键。
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@acctPeriodascending:NO];
NSArray * sortDescriptors = @ [sortDescriptor];

//谓词
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:@periodYearcacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

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

return _fetchedResultsController;
}


解决方案

在您的托管对象子类的类扩展中的 periodYear 方法(将使用
作为节名称键路径):

  @interface事件(AdditionalMethods)
- (NSString *)periodYear;
@end

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

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

  NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@acctPeriodascending: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 作为管理对象的 transient属性
在这种情况下,它也不会存储在数据库中,但可以按照需要计算并缓存的方式实现。



来自Apple开发人员库的 DateSectionTitles 示例项目演示了如何工作。 p>

I am new at core data and am trying to figure out how to create a custom sectionNameKeyPath in my NSFetchedResultsController. I have a managed object with an attribute called acctPeriod. It is a NSString. I want to create sections based on the first 4 characters of this field. The first 4 characters represent the year of the accounting period and doesn't need to be saved.

I have gone through this site and have seen posts about transient attributes but I can't seem to get them to work. Basically I want this and then assign periodYear for my sectionNameKeyPath.

@dynamic periodYear;

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

Any help would be appreciated.

**UPDATE: Using Martin R. answer, I was able to get it to work as expected.

- (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;  
}

解决方案

The following should work: Implement the periodYear method (which will be used as "section name key path") in a class extension of your managed object subclass:

@interface Event (AdditionalMethods)
- (NSString *)periodYear;
@end

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

Make sure that acctPeriod is used as the first (or only) sort descriptor for the fetch request:

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

Use periodYear as sectionNameKeyPath for the fetched results controller:

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

And finally add the default titleForHeaderInSection method:

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

Alternatively, you can define periodYear as transient attribute of the managed object. It will also not be stored in the database in that case, but can be implemented in a way that the value is calculated on demand and cached.

The DateSectionTitles sample project from the Apple Developer Library demonstrates how this works.

这篇关于自定义Core数据SectionNameKeyPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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