Core Data: changes in one UITableView/entity not b

2019-04-17 08:10发布

问题:

         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)

回答1:

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.



回答2:

I had the same error message from Core Data but this was the solution.

https://developer.apple.com/library/content/releasenotes/iPhone/NSFetchedResultsChangeMoveReportedAsNSFetchedResultsChangeUpdate/