Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why NSFetchedResultsController is not being updated with new data?

My Core Data model has two entities: Author and Book with a To-Many relationship (one author->many books). In the main view I display a list of books where each cell contains book name and author name. The view is also divided into sections where each section title is the author name. (note that "author.name" is set for both sort descriptor and sectionNameKeyPath)

Here is the code (simplified for clarity):

- (NSFetchedResultsController *)fetchedResultsController {

    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }
    
    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"author.name" ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
    
    [fetchRequest setSortDescriptors:sortDescriptors];
    
    NSFetchedResultsController *aFetchedResultsController = [[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"author.name" cacheName:nil] autorelease];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
    
    NSError *error = nil;
    [self.fetchedResultsController performFetch:&error];
    
    return __fetchedResultsController;
}    

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

     Book* book = [self.fetchedResultsController objectAtIndexPath:indexPath];
     cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", book.name, book.author.name];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller {

    [self.tableView reloadData];
}

Now, if the user changes the author name and then goes back to main view, the cells and sections will display the old author name. After searching the Internet, I found the following code which fixes the old author name issue in the cells but not in section titles:

- (void)saveAuthorName:(NSString *)newName {
    for (Book* book in author.books) {
        [book willChangeValueForKey:@"author"];
    }
     
    author.name = newName;

    for (Book* book in author.books) {
        [book didChangeValueForKey:@"author"];
    }
    
    // save changes
    NSError * error ;
    if( ![self.moc save:&error] ) {
        // Handle error
    } 
}

Why is [self.fetchedResultsController sections] still contains old author names? Please help!

Update #1

This section relates to Response #1 of Marcus

Hmmm, still a little fuzzy. Are you saying the number of sections is incorrect?

The number of sections was not changed. The content of the objects in the Sections property array is incorrect.

Based on your code posted you are simply retrieving the NSManagedObject instances from the NSFetchedResultsController. Perhaps there is some confusion as to what that is?

In the code, I am retrieving the NSManagedObject instances from the NSFetchedResultsController in order to display book name and author name for each table cell (when cellForRowAtIndexPath is called). However, the headers of each section in UITableView are not taken from NSManagedObject to my understanding but are taken from _NSDefaultSectionInfo object which implements NSFetchedResultsSectionInfo protocol (when titleForHeaderInSection is called).

I realized this by the following code I wrote for debugging:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    id mySection = [[fetchedBooks sections] objectAtIndex:section];
    NSLog(@"%@", mySection)

    return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}

The result of the log was <_NSDefaultSectionInfo: 0x8462b90>. NSFetchedResultsController documentation for Sections property shows:

/* Returns an array of objects that implement the NSFetchedResultsSectionInfo protocol.
   It's expected that developers use the returned array when implementing the following methods of the UITableViewDataSource protocol

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; 

*/

So please correct me if I am wrong: _NSDefaultSectionInfo is not an NSManagedObject right? If so, how can NSFetchedResultsController detect changes for _NSDefaultSectionInfo objects when Author NSManagedObject are changed?

So this leads to a couple of questions:

How are you changing the author's name in this other view?

The code for changing the author's name is written above in saveAuthorName. The flow in the App is as follows:

  • From the main UITableView, selecting the book cell which opens new Book view using navigation controller.

  • From the book view, choosing select author which opens new Select-Author view using navigation controller. (all authors are listed in a UITableView)

  • From Select-Author view, selecting any author which opens new Edit-Author view using navigation controller.

  • In the Edit-Author view changing author name and saving which closes view and bring the previous view (Select-Author) in navigation controller stack.

  • Now it's possible to select different author and editing it and so on.. until closing this view. (Brings Book view)

  • Closing Book view, brings to main view where all books are displayed.

Is the author name in the cell old or just the section header?

Cell is perfectly updated with author name (thanks to the willChangeValueForKey and didChangeValueForKey called in saveAuthorName). Only section header is old.

What do your delegate methods look like?

could you please specify which one exactly? I wrote all delegate methods that looks relevant to me in the above code section. This includes:

  • cellForRowAtIndexPath

  • titleForHeaderInSection

  • controllerDidChangeContent

Any other method is required?

Are you certain that your -[UITableViewDatasource tableView: titleForHeaderInSection:] is firing after you return from the edit?

100% percent sure. titleForHeaderInSection brings old values and it is being called after changes were saved. (cellForRowAtIndexPath is also called after changes were saved but is bringing new values)

What NSFetchedResultsControllerDelegate methods are firing upon the return?

If you mean upon saving (means, after saveAuthorName is called) following methods being called:

  • controllerWillChangeContent: (not using it, just for debug info)

  • controller:didChangeObject: (not using it, just for debug info)

  • controllerDidChangeContent:

If you mean upon returning to main view (means, closing the Book view) following methods being called:

  • cellForRowAtIndexPath

  • titleForHeaderInSection

  • numberOfSectionsInTableView

  • numberOfRowsInSection

I appreciate your help. Thanks!

Update #2

Are you implementing -controller: didChangeSection: atIndex: forChangeType:?

Yes I do, but is does not being fired when changing the author name. The current configuration for the NSFetchedResultsController is as follows:

  • Entity: Book
  • Sort Descriptor: author.name
  • sectionNameKeyPath: author.name

Changing a book name (rather than author name) will fire didChangeSection event when NSFetchedResultsController is configured as follows:

  • Entity: Book
  • Sort Descriptor: name
  • sectionNameKeyPath: name

Which means that the delegate is properly hooked to the NSFetchedResultsController.

It looks as calling [book willChangeValueForKey:@"author"] and [book didChangeValueForKey:@"author"] when changing author name is not enough for NSFetchedResultsController in order to monitor section changes.

like image 808
Joshua Avatar asked Feb 25 '12 18:02

Joshua


2 Answers

Generally you should not need to save the changes if you are dealing with a single NSManagedObjectContext for both the NSFetchedResultsController and the UIViewController that is making the changes.

That does not apply if you have more than one NSManagedObjectContext.

Assuming you have one NSManagedObjectContext, I would make sure you have the delegate set on the NSFetchedResultsController and put break points in the delegate methods to see if you are getting any callbacks at all.

I would also use the debugger and print out the pointers for the NSManagedObject(s) you are working with and make sure they are the same. If they are not then it would point to an issue with the NSManagedObjectContext.

Response #1

Hmmm, still a little fuzzy. Are you saying the number of sections is incorrect?

Based on your code posted you are simply retrieving the NSManagedObject instances from the NSFetchedResultsController. Perhaps there is some confusion as to what that is?

The NSFetchedResultsController is merely a container that has one or more sections. Those sections are also just a container that holds one or more NSManagedObject instances. Those are the same instances of NSManagedObject that you would access anywhere else in your application (assuming a single NSManagedObjectContext design).

Therefore if you change the data in a NSManagedObject anywhere in your application it will be updated in the NSFetchedResultsController because it is the same object, not a copy but the exact same object.

So this leads to a couple of questions:

  1. How are you changing the author's name in this other view?
  2. Is the author name in the cell old or just the section header? 
  3. What do your delegate methods look like?
  4. Are you certain that your -[UITableViewDatasource tableView: titleForHeaderInSection:] is firing after you return from the edit?
  5. What NSFetchedResultsControllerDelegate methods are firing upon the return?

Response #2

Are you implementing -controller: didChangeSection: atIndex: forChangeType:? If not, please do so and tell me if it fires. If it does fire, when does it fire? Before or after the call to -[UITableViewDatasource tableView: titleForHeaderInSection:]?

Response #3

This is starting to sound like an Apple bug.

A couple of thoughts:

  1. What happens if you do a -performFetch: after the author is tickled? I wonder if that would work.
  2. I strongly suggest that you create a test case for this. You can then submit it to Apple for a radar (vital) and I can play with the test also and see if there is a clean solution.
like image 144
Marcus S. Zarra Avatar answered Nov 18 '22 19:11

Marcus S. Zarra


Today with Swift everything is easier.

  1. You implement NSFetchedResultsControllerDelegate in your controller
  2. Set your controller as delegate of your NSFetchedResultsController.
  3. Do not use performFetch on your fetchedResultsController, it swallows up the event which normally goes to the delegate.
  4. Change a Managed object -> delegate methods should be called.
like image 20
Fabian Avatar answered Nov 18 '22 20:11

Fabian