关系未更新的 NSFetchedResultsController [英] NSFetchedResultsController with relationship not updating

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

问题描述

假设我有两个实体,EmployeeDepartment.一个部门与一个员工是一对多的关系,一个部门可以有多个员工,但每个员工只属于一个部门.我想使用 NSFetchedResultsController 在按数据排序的表视图中显示所有员工,这些数据是他们所属部门的属性.问题是我希望我的表在部门对象收到更改时更新,就像员工的常规属性更改时一样,但 NSFetchedResultsController 似乎没有跟踪相关对象.我通过执行以下操作部分解决了这个问题:

Let's say I have two entities, Employee and Department. A department has a to-many relationship with an 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 table view 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 NSFetchedResultsController 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];

这很好用,并且在第一次调用时正确地对员工进行了排序,问题是当我对部门的某些属性进行更改时,应该更改我的员工表的排序,但没有任何反应.有什么好的方法可以让我的员工 FRC 跟踪关系中的变化吗?特别是当排序基于相关属性时,我只需要某种方式让它更新排序.我已经浏览了一些类似的问题,但没有找到满意的解决方案.

This works great and sorts the employees correctly the first time it's called, the problem is that when I make changes to some property of a department 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 questions but wasn't able to find a satisfactory solution.

推荐答案

NSFetchedResultsController 仅设计用于一次观察一个实体.您的设置虽然有意义,但有点超出了 NSFetchedResultsController 目前能够自行观察的范围.

The NSFetchedResultsController is 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 straightforward.

基本上,您希望查看 NSManagedObjectContextDidSaveNotification 发布的信息,然后在发生包含您的部门实体的火灾时重新加载您的表格.

Basically, you want to watch for NSManagedObjectContextDidSaveNotification postings and then reload your table when one fire 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天全站免登陆