更改sectionNameKeyPath属性值不会反映在NSFetchedResultsController上 [英] Changed sectionNameKeyPath attribute value is not reflected on NSFetchedResultsController

查看:69
本文介绍了更改sectionNameKeyPath属性值不会反映在NSFetchedResultsController上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由NSFetchedResultsController支持的表视图。



当底层上下文中的对象发生变化时,NSFetchedResultsController会自动在表上反映新的属性值,视图。 Yay。



我注意到的一个例外是用于sectionNameKeyPath的属性值的更新不会自动反映。



我猜,用于sectionNameKeyPath的属性值是NSFetchedResultsController的基础,我需要执行获取&

更新:用于配置获取请求的代码

   - (void)configureFetch {

CoreDataHelper * cdh = [(AppDelegate *)[[UIApplication sharedApplication] delegate] cdh];

NSFetchRequest * request =
[NSFetchRequest fetchRequestWithEntityName:@Item];

request.sortDescriptors =
[NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@locationAtHome.storedIn
ascending:YES],
[NSSortDescriptor sortDescriptorWithKey :@name
ascending:YES],
nil];
[request setFetchBatchSize:15];
self.frc =
[[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:cdh.context
sectionNameKeyPath:@locationAtHome.storedIn
cacheName:nil];
self.frc.delegate = self;
}

以及执行提取的代码:

   - (void)performFetch {

if(self.frc){
[self.frc.managedObjectContext performBlockAndWait:^ {
NSError * error;
[self.frc performFetch:& error];
if(error)NSLog(@%@'%@'%@(原因:%@),
self.class,NSStringFromSelector(_cmd),
error.localizedDescription,error .localizedFailureReason);

[self.tableView reloadData];
}];
}
}


解决方案

问题不是改变 sectionNameKeyPath 所使用的属性,而是更改相关对象中的属性。



提取的结果控制器仅跟踪对获取的对象本身(您的案例中的 Item 对象)的更改,而不跟踪相关对象的更改。



唯一的解决方案(就我所知),是将该部分作为附加属性添加到实体,即使这意味着您有重复的数据。 / p>

I have a table-view backed by an NSFetchedResultsController.

Any time an object in the underlying context changes, the NSFetchedResultsController automatically reflects new attribute values on the table-view. Yay.

The one exception I have noticed is that updates to the attribute value used for sectionNameKeyPath are not reflected automatically.

I'm guessing the attribute value used for the sectionNameKeyPath is so fundamental to the NSFetchedResultsController that I'll need to performFetch & reload the table-view again?

Update: heres the code used to configure the fetch request

- (void)configureFetch {

CoreDataHelper *cdh = [(AppDelegate *)[[UIApplication sharedApplication] delegate] cdh];

NSFetchRequest *request =
[NSFetchRequest fetchRequestWithEntityName:@"Item"];

request.sortDescriptors =
[NSArray arrayWithObjects:
 [NSSortDescriptor sortDescriptorWithKey:@"locationAtHome.storedIn"
                               ascending:YES],
 [NSSortDescriptor sortDescriptorWithKey:@"name"
                               ascending:YES],
 nil];
[request setFetchBatchSize:15];
self.frc =
[[NSFetchedResultsController alloc] initWithFetchRequest:request
                                    managedObjectContext:cdh.context
                                      sectionNameKeyPath:@"locationAtHome.storedIn"
                                               cacheName:nil];
self.frc.delegate = self;
}

and the code to perform the fetch:

- (void)performFetch {

if (self.frc) {
    [self.frc.managedObjectContext performBlockAndWait:^{
        NSError *error;
        [self.frc performFetch:&error];
        if (error) NSLog(@"%@ '%@' %@ (Reason: %@)",
                         self.class, NSStringFromSelector(_cmd),
                         error.localizedDescription, error.localizedFailureReason);

        [self.tableView reloadData];
    }];
}
}

解决方案

The problem is not that the attribute used as sectionNameKeyPath is changed, but that an attribute in a related object is changed.

A fetched results controller tracks only changes to the fetched objects itself (the Item objects in your case), but not to related objects.

The only solution (as far as I know), is to add the section as an additional attribute to the Item entity, even if that means that you have duplicate data.

这篇关于更改sectionNameKeyPath属性值不会反映在NSFetchedResultsController上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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