核心数据:一个 UITableView/实体中的更改未被其他“间接”识别相关UItableviews/实体

发布于 2024-12-11 17:50:24 字数 1297 浏览 0 评论 0原文

         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() 通过模型中的“对多”关系访问课程。 (即 course_rel)。所有这些对于两个视图的初始显示都适用。

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

这是我的问题。当用户通过 vc3 添加新课程,然后切换到 vc2 时,新课程不会显示,并且出现下面提供的错误。我知道我收到错误,因为更改是在课程数据库表中进行的,但 vc2 中的表视图是从“years”表加载的。它永远不会收到“didChangeObject”和其他相关消息,因为它处理的是“年份”而不是课程。然而,每个分组部分的主体是课程列表!

当用户转到 vc2 时,如何立即反映课程表 (vc3) 中的更改?

我在 viewWillLoad 函数中放置了一个新的提取操作和一个 [view reloadData] ,但同样,这是多年的提取,而不是课程的提取,因此它没有效果。

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

CoreData:错误:严重的应用程序错误。捕获了异常 在调用期间从 NSFetchedResultsController 的委托 -controllerDidChangeContent:。无效更新:第 0 节中的行数无效。现有节中包含的行数 更新后 (3) 必须等于包含的行数 更新之前的部分 (2),加上或减去行数 从该部分插入或删除(0 插入,0 删除)并加上 或减去移入或移出该部分的行数(0 移动 入,0 移出)。与用户信息(空)

         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

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 also works great. The user can add a new course to any selected year.

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!

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

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.

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飞烟轻若梦 2024-12-18 17:50:24

您对 VC2 的获取请求应该是“课程”请求,而不是“学年”请求。仅当从其获取请求返回的结果集发生更改时,获取的结果控制器才会注意到更改并调用您的委托方法。添加新课程时,不会创建新的年份,因此获取的结果控制器不会传播任何更改。

假设您的课程实体具有“年”关系,那么您可以通过像这样初始化它来将获取的结果控制器配置为按年分组:

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

然后更改您的表视图数据源方法以使用 frc 中的部分信息来创建年份部分并用课程填充这些部分。如果您这样做,那么每当添加新课程时,frc 都会通过委托方法通知 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>];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文