Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7591347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:37:48+00:00 2026-05-30T20:37:48+00:00

My Core Data model has two entities: Author and Book with a To-Many relationship

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:37:49+00:00Added an answer on May 30, 2026 at 8:37 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Core Data model I have two entities: List and Patient. List has
I have created a core data model that has two entities which have a
I have a core data model that has two entities, Bid and Result. I
I have a Core Data model with two entities: A and B. A has
My Core Data model contains an entity, Shape, that has two self-referential relationships, which
I have two entities in a Core Data Model like these: A <<--->> B
In my core data model I have a Person entity that has a to
I have a relationship in a Core Data model that feels like it wants
I have set up a Core Data model where I have two objects, say
I am trying to re-call core-data in UITable using model view. I am in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.