16

I can create new managed objects inside my app, in a separate view I have a table view which shows all the objects I have.

When I create a new managed object (in a totally separate view), I am sending out a notification which is trying to get my table view to reload with the new data which includes the new object i just made.

I'm not editing the table view.

There is only one managed context.

I can't grasp what I am doing wrong. If I close the app and relaunch my new object is in the table view. I am trying to call reloadData when I get the notification a new object has been made, and I've also tried performing the fetch again, but all I end up with is an empty table view.

In terms of code, my ListViewController:

@interface MyNotesViewController : UIViewController
{
    NSManagedObjectContext * __managedObjectContext;
    UITableView * _notesTableView;
    MyNotesTableViewDelegate_DataSource * _tableDelegate_DataSource;
}

My viewDidLoad looks like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _tableDelegate_DataSource = [[MyNotesTableViewDelegate_DataSource alloc] init];
    _tableDelegate_DataSource.managedObjectContext = __managedObjectContext;
    [_notesTableView setDataSource:_tableDelegate_DataSource];

    NSError * _coreDataError;
    BOOL success = [[_tableDelegate_DataSource fetchedResultsController] performFetch:&_coreDataError];

    if(success){
        [_notesTableView reloadData];
    }
}

In my delegate/datasource object I have:

@interface MyCommutesTableViewDelegate_DataSource : NSObject 

<UITableViewDataSource, NSFetchedResultsControllerDelegate>
{
    NSManagedObjectContext * __managedObjectContext;
    NSFetchedResultsController * __fetchedResultsController;
}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

@end

Implementation excerpt:

-(NSFetchedResultsController*)fetchedResultsController{
    if(!__fetchedResultsController){
        NSFetchRequest * request = [[NSFetchRequest alloc] init];
        [request setEntity:[NSEntityDescription entityForName:@"Note" inManagedObjectContext:__managedObjectContext]];

        [request setFetchBatchSize:10];

        NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"noteDate" ascending:NO];
        [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
        [sortDescriptor release];

        __fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:__managedObjectContext sectionNameKeyPath:nil cacheName:nil];


        __fetchedResultsController.delegate = self;
        [request release];

    }
    return __fetchedResultsController;
}

I have the stander UITableView data source methods here:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    id <NSFetchedResultsSectionInfo> sectionInfo = 
    [[__fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

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

    static NSString *CellIdentifier = @"NotesTableViewCell";

    NotesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {        
        NSArray * topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NotesTableViewCell" owner:nil options:nil];
        for(id currentObject in topLevelObjects){
            if([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (NotesTableViewCell*) currentObject;
                break;
            }
        }

    }

// Configure the cell.
    Note * _note = [__fetchedResultsController objectAtIndexPath:indexPath];

    [...]

    return cell;
}

UPDATE

I reverted to simply requesting all objects because I had to quick fix my problem, and then I realised, after I save my context, my fetch request I had previously used on the app launch, now returns no data, there's no error but it just returns nothing.

So I guess there's no issue with how I'm implementing NSFetchResultsController. But how could the request start not returning results after I save a new object ?

Note I still get all of my objects if I relaunch, including the newly added one.

standard request:

-(NSArray*)getNotes{
    NSFetchRequest * request = [[NSFetchRequest alloc] init];

    NSEntityDescription * entity = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:[self managedObjectContext]];
    [request setEntity:entity];

    NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"noteDate" ascending:NO];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    [sortDescriptor release];

    NSError * coreDataError;
    NSArray * fetchedObjects = [[self managedObjectContext] executeFetchRequest:request error:&coreDataError];
    [request release];

    if(fetchedObjects != nil){
        return fetchedObjects;
    } else {
        NSLog(@"ERR: %@", coreDataError);
        return nil;
    }
}

I am waiting for a notification telling me a new object has been added to Core Data, and then I am calling the above method and then calling reloadData on my tableview...

Daniel
  • 23,129
  • 12
  • 109
  • 154

4 Answers4

29

You are setting your UITableViewController as the NSFetchedResultsControllerDelegate. That's good. Now try to implement the controllerDidChangeContent: method in the TableViewController like so:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
   [self.tableView reloadData];
}

Your NSFetchedResultsController will listen to removed or new objects in Core Data and notify its delegate (your TableViewController) of changes. Check the Core Data template project in Xcode to implement this even better with add and removal animations in the UITableView.

Daniel
  • 23,129
  • 12
  • 109
  • 154
huesforalice
  • 2,418
  • 2
  • 24
  • 29
  • of course you'll want to reload the _notesTableView in your case. – huesforalice Oct 20 '11 at 20:42
  • 10
    Also do not forget that the `NSFetchedResultsController` will only react to `-save:` being called on the `NSManagedObjectContext`. So you may not see this method fire if you are not saving the newly created entity. – Marcus S. Zarra Oct 20 '11 at 21:41
  • Thanks, I'm implementing controllerDidChangeContent: from the NSFetchedResultsControllerDelegate protocol, calling reloadData on my tableview. I've added breakpoints and this method is being called, but the tableview is reloading empty. If I relaunch the app, I can see all my records including the new one. What am I missing? PS: do I have to implement controller:didChangeObject:atIndexPath:forChangeType:newIndexPath ? – Daniel Oct 20 '11 at 21:42
  • @MarcusS.Zarra it seems this is being triggered as soon as I create the Managed Object, not when I save, which was a pain because I was creating my object and adding data to it on the fly, which was causing a crash because the tableview needed to load information to display in the cell, before this information was appended to the object – Daniel Oct 20 '11 at 21:43
  • You don't need to implement to implement the method you mentioned, you should be fine with the one I suggested. I'm not sure why you get an empty tableview. To me your code seems fine otherwise. – huesforalice Oct 21 '11 at 10:19
  • controllerDidChangeContent: is being called as soon as I create a new Managed Object, not when I save: the context for some reason, I found a post, it may be an Apple bug ? http://openradar.appspot.com/10207615 – Daniel Oct 21 '11 at 10:44
  • I tried skipping NSFetchedResultsController and I'm just fetching the array of objects from Core Data, I'm getting the same problem, when I launch the app all loads well. When I add an object to Core Data, the fetch returns no objects at all, and if I relaunch the app, I see everything including new object. So I must be doing something wrong elsewhere ? My Managed Object is a subclass, maybe I should refer to it as NSManagedObject during creation and only fetch is as my custom class ? – Daniel Oct 21 '11 at 12:58
  • 1
    You have to re-fetch after a context change. So back to using the NSFetchedResultsController, you would do: `- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.fetchedResultsController performFetch:&error]; [self.tableView reloadData]; }` – cocoahero Oct 21 '11 at 13:12
  • IF YOU CHANGE THE SORTING, HOW TO MAKE EFFECTIVE THIS CHANGE? THE controllerDidChangeContent NOT DETECT IT. – Markus Oct 11 '17 at 15:51
2

Be sure to add the didChangeObject method:

- (void)controller:(NSFetchedResultsController *)controller
   didChangeObject:(id) anObject
       atIndexPath:(NSIndexPath *)indexPath
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath 
{
  switch(type) 
  {
    case NSFetchedResultsChangeInsert:
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                            withRowAnimation:UITableViewRowAnimationFade];
      break;
    case NSFetchedResultsChangeDelete:
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                            withRowAnimation:UITableViewRowAnimationFade];
      break;
    case NSFetchedResultsChangeUpdate:
      [self configureCell:[self.tableView
    cellForRowAtIndexPath:indexPath]
              atIndexPath:indexPath];
      break;
    case NSFetchedResultsChangeMove:
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                            withRowAnimation:UITableViewRowAnimationFade];
      [self.tableView insertRowsAtIndexPaths:[NSArray
                             arrayWithObject:newIndexPath]
                            withRowAnimation:UITableViewRowAnimationFade];
      break;
  }
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{
  NSManagedObject *note = [self.fetchedResultsController objectAtIndexPath:indexPath];
  cell.textLabel.text = [note valueForKey:@"title"];
}

The new managed object showed up in the table view after this.

codiac
  • 71
  • 8
2

Updated to latest swift 4.2 and Xcode 10.

extension MyListController: NSFetchedResultsControllerDelegate {
    func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
        self.tableView.beginUpdates()
    }

    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {

        switch type {
        case .insert:
            self.tableView.insertRows(at: [newIndexPath!], with: .fade)
        case .delete:
            self.tableView.deleteRows(at: [indexPath!], with: .fade)
        case .update:
            self.tableView.reloadRows(at: [indexPath!], with: .fade)
        case .move:
            self.tableView.deleteRows(at: [indexPath!], with: .fade)
            self.tableView.insertRows(at: [indexPath!], with: .fade)
        }
    }


    func controller(_ controller: 
        NSFetchedResultsController<NSFetchRequestResult>, didChange 
        sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex 
        sectionIndex: Int, for type: NSFetchedResultsChangeType) {

        switch (type) {
        case .insert:
            self.tableView.insertSections([sectionIndex], with: .fade)
        case .delete:
            self.tableView.deleteSections([sectionIndex], with: .fade)
        case .move:
            self.tableView.deleteSections([sectionIndex], with: .fade)
            self.tableView.insertSections([sectionIndex], with: .fade)
        case .update:
            self.tableView.reloadSections([sectionIndex], with: .fade)
        }
    }

    func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
        self.tableView.endUpdates()
    }
}
Phuah Yee Keat
  • 1,572
  • 1
  • 17
  • 17
0

In case someone having the same issue, as I just had it. I have tried the above solution with green tick, but it wouldn't work for me. I have followed Apple's code and everything went fine.

simply, just implement the three "Fetchcontroller" delegate functions

    func controllerWillChangeContent(controller: NSFetchedResultsController) {
            self.tableView.beginUpdates()
        }


   func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {

            switch type {
            case .Insert:
                self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
            case .Delete:
                self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
            case .Update:
                print("")
                self.configureCell(self.tableView.cellForRowAtIndexPath(indexPath!)!, indexPath: indexPath!)
            case .Move:
                self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
                self.tableView.insertRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
            }
        }


 func controllerDidChangeContent(controller: NSFetchedResultsController) {
            self.tableView.endUpdates()
        }
Ofcourse
  • 617
  • 1
  • 7
  • 19