核心数据:一个UITableView /实体中的变化不被其他“间接”相关UItableviews /实体 [英] Core Data: changes in one UITableView/entity not being recognized by other "indirectly" related UItableviews/entity

查看:149
本文介绍了核心数据:一个UITableView /实体中的变化不被其他“间接”相关UItableviews /实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

         appdelegate
     passes modelview 
     to each tab controller
            |
            |
          /    \
         /      \   
        /        \
       /          \ 
      /            \
     /              \
    vc1             vc2
 show list      show grouped  
 of years     table with years
    |          as headers and
    |           courses within
   vc3            each year
list courses
in selected 
year
   |
   |
add new 
course to
selected 
year

在vc1和vc2中,我从我的核心数据模型中读取的实体是年。在numberOfRowsInSection()等等例程中,我通过NSSet()在我的模型中通过to-many关系访问课程。 (即courses_rel)。所有这些都适用于两个视图的初始显示。

In both vc1 and vc2, the entity that I am reading from my core data model is "years". In the "numberOfRowsInSection()", etc routines, I access the courses through "to-many" relationships in my model, via NSSet(). (i.e. courses_rel). All this works fine for the initial display of both views.

vc3也很好。用户可以向任何选定的年份添加新课程。

vc3 also works great. The user can add a new course to any selected year.

这是我的问题。当用户通过vc3添加新课程,然后切换到vc2,新课程不显示,我得到下面提供的错误。我知道我得到错误,因为更改是在课程数据库表中进行,但vc2中的表视图正从年表中加载。它从来没有得到didChangeObject和其他相关的消息,因为它是处理年,而不是课程。但是,每个分组部分的正文是一个课程列表!

Here is my problem. When the user adds a new course via vc3, then switches over to vc2, the new course does not show up and I get the error provided below. I know that I'm getting the error because the change was made in the courses database table but the tableview in vc2 is being loaded from the "years" table. It never gets the "didChangeObject" and other relevant messages since it is dealing with "years" and not courses. However, the body of each grouped section is a list of courses!

如何获得课程表(vc3)的更改,以便在用户转到vc2?

How do I get a change in courses table (vc3) to be reflected immediately when the user goes to vc2?

我在viewWillLoad函数中放置了一个新的fetch操作和一个[view reloadData],但是它又是一个fetch的年,不是课程,所以它没有

I have placed a new fetch operation and a [view reloadData] in the viewWillLoad function but again, it is a fetch of years and not courses so it has no effect.

这是当用户在vc3中添加课程后切换到vc2时获得的错误。

Here is the error I get when the user switches over to vc2 after adding a course in vc3.

CoreData:错误:严重的应用程序错误。在调用
-controllerDidChangeContent:时,从NSFetchedResultsController的委托捕获到
异常。无效更新:区段0中的行数无效。更新(3)后,现有区段中包含的行数
必须等于
中包含的行数(更新前的区段) 2),加或减从该节插入或删除的行数
(插入0,删除0)和加
或减去移入或移出该节的行数(0移动
in,0移出)。 with userInfo(null)

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)


推荐答案

VC2的获取请求应该是课程请求不是'年'的要求。提取的结果控制器只会注意到更改,如果从其提取请求返回的结果集更改,则调用您的委托方法。当您添加新课程时,不会创建新的年份,因此获取的结果控制器不会传播任何更改。

Your fetch request for VC2 should be a 'courses' request not a 'years' request. A fetched results controller will only notice changes and call your delegate methods if the results set returned from its fetch request changes. When you add a new course, no new years are created so the fetched results controller is not propagating any changes.

假设您的课程实体具有年你可以配置你抓取的结果控制器按照以下方式逐个分组:

Assuming that your course entity has a 'year' relationship then you can configure your fetched results controller to group by year by init'ing it like this:

 NSFetchedResultsController *myController = [[NSFetchedResultsController alloc] 
                                 initWithFetchRequest:<a courses fetch request> 
                                 managedObjectContext:context
                                   sectionNameKeyPath:@"year"
                                            cacheName:<cache name or nil>];

然后更改您的tableview数据源方法以使用frc中的节信息创建年份的节并填充与课程的部分。如果你这样做,那么每当一个新的课程添加frc将通过委托方法通知vc2。

Then alter your tableview datasource methods to use the section information from the frc to create sections for years and populate the sections with courses. If you do it this way then whenever a new course is added the frc will notify vc2 via the delegate methods.

这篇关于核心数据:一个UITableView /实体中的变化不被其他“间接”相关UItableviews /实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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