NSFetchedResultsChangeDelete not starting

Has anyone come across this before?

When I choose to remove a row from the tableView table (filled with FRC), the application does not crash or freeze. He does not do anything. The delete button remains selected, and if I click elsewhere in the simulator, the delete button deselects and disappears, but the cell is never deleted from the user interface. I am sure that there is stupid oversight here, but I cannot understand it. Below are the relevant parts of my code.

I have a UITableViewController interface declared as such:

#import <UIKit/UIKit.h>
#import "RubricAppDelegate.h"


@interface ClassList : UITableViewController <NSFetchedResultsControllerDelegate> {
    NSMutableArray *classList;
    NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;

}

@property(nonatomic,retain) NSMutableArray *classList;
@property(nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property(nonatomic, retain) NSManagedObjectContext *managedObjectContext;

- (IBAction) grade:(id)sender;

@end

In my implementation file, I have:

- (void)viewDidLoad {

    [super viewDidLoad];

    RubricAppDelegate *appDelegate = (RubricAppDelegate *)[[UIApplication sharedApplication] delegate];
    managedObjectContext = [appDelegate managedObjectContext];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"myClass" inManagedObjectContext:managedObjectContext];
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    [request setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"classID" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptor release];


    fetchedResultsController = [[NSFetchedResultsController alloc]
                                           initWithFetchRequest:request 
                                           managedObjectContext:self.managedObjectContext
                                           sectionNameKeyPath:nil cacheName:nil];
    NSError *error;
    [fetchedResultsController performFetch:&error];
}

and

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        myClass *result = (myClass *)[fetchedResultsController objectAtIndexPath:indexPath];
        [managedObjectContext deleteObject:result]; 
            NSError *error;
            [managedObjectContext save:&error]; 
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        //Not yet implemented   
    }   
}

and

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:

            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];

            break;

        case NSFetchedResultsChangeDelete:

            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                 withRowAnimation:UITableViewRowAnimationFade];

            break;

        case NSFetchedResultsChangeUpdate:

            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];

            break;

        case NSFetchedResultsChangeMove:

            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];

            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
                             withRowAnimation:UITableViewRowAnimationFade];

            break;
    }

}

Besides

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == [[fetchedResultsController fetchedObjects] count]) {
        return UITableViewCellEditingStyleInsert;
    }
    return UITableViewCellEditingStyleDelete;
}

This code only makes the cell after my cells filled with cells, the insert cell, but the rest remove the cells.

UITableView, IB. , NSFetchedResultsChangeDelete:?

UPDATE:

[managedObjectContext save:&error]; commitEditingStyle , , . , , , , , , .

, "", - . , , .

+3
2

, NSFetchedResultsController? , @DVG, . , .

Update

, NSFetchedResultsController:

[fetchedResultsController setDelegate:self];

, -performFetch:.

+2

, editStyleForRowAtIndexPath, , , .

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  return UITableViewCellEditingStyleDelete;
}

, , ObjectContext.

EDIT: , , , .

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

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

Source: https://habr.com/ru/post/1757259/


All Articles