NSFetchedResultsController与关系不更新 [英] NSFetchedResultsController with relationship not updating

查看:65
本文介绍了NSFetchedResultsController与关系不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个实体,员工和部门。部门与员工有一对多的关系,许多员工可以在每个部门,但每个员工只属于一个部门。我想使用NSFetchedResultsController来显示一个tableview中的所有员工,这些数据按照属于它们所属部门的属性的数据排序。问题是,我想要我的表更新时,部门对象接收更改,就像它是如果员工的常规属性更改,但FetchedResultsController似乎没有跟踪相关对象。我通过执行以下操作部分通过了此问题:

Let's say I have two entities, employee and department. A department has a to-many relationship with employee, many employees can be in each department but each employee only belongs to one department. I want to display all of the employees in a tableview sorted by data that is a property of the department they belong to using an NSFetchedResultsController. The problem is that I want my table to update when a department object receives changes just like it does if the regular properties of employee change, but the FetchedResultsController doesn't seem to track related objects. I've gotten passed this issue partially by doing the following:

for (Employee* employee in department.employees) {
    [employee willChangeValueForKey:@"dept"];
}

/* Make Changes to department object */

for (Employee* employee in department.employees) {
    [employee didChangeValueForKey:@"dept"];
}

这显然不是理想的,但它确实导致基于员工的FRC委托方法didChangeObject被调用。我现在剩下的真正的问题是在排序跟踪员工对象的FRC:

This is obviously not ideal but it does cause the employee based FRC delegate method didChangeObject to get called. The real problem I have left now is in the sorting a FRC that is tracking employee objects:

NSEntityDescription *employee = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"department.someProperty" ascending:NO];

这是非常好的,并在第一次调用时正确排序员工,问题是,改变一个部门someProperty应该改变我的employee表的排序没有什么发生。有没有什么好的方法让我的员工FRC跟踪在关系中的变化?特别是我只需要一些方法来更新排序,当排序是基于相关的属性。

This works great and sorts the employees correctly the first time it's called, the problem is that when I make changes to a departments someProperty that should change the sorting of my employee table nothing happens. Is there any nice way to have my employee FRC track changes in a relationship? Particularly I just need some way to have it update the sorting when the sort is based on a related property. I've looked through some similar question but wasn't able to find a satisfactory solution.

推荐答案

NSFetchedResultsController 实际上只设计为一次观看一个实体。你的设置,虽然有意义,但它有点超出了 NSFetchedResultsController 当前能够自己观察的范围。

The NSFetchedResultsController is really only designed to watch one entity at a time. Your setup, while it makes sense, it a bit beyond what the NSFetchedResultsController is currently capable of watching on its own.

我的建议是设置自己的观察者。您可以根据我在 GitHub 上设置的ZSContextWatcher设置,或者使其更简单。

My recommendation would be to set up your own watcher. You can base it off the ZSContextWatcher I have set up on GitHub or you can make it even more simple.

基本上你想要监视 NSManagedObjectContextDidSaveNotification 过帐,然后在包含你的部门实体的一个账户触发时重新载入你的表。

Basically you want to watch for NSManagedObjectContextDidSaveNotification postings and then reload your table when one fires that contains your department entity.

我还建议您向Apple提交 rdar ,并要求您提供 NSFetchedResultsController 需要改进。

I would also recommend filing a rdar with Apple and asking for the NSFetchedResultsController to be improved.

这篇关于NSFetchedResultsController与关系不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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