0
votes

My tableView have few section and for the second section I have NSFetchedResultsController. I'm having issues refreshing my table view after deleting the row, from section2.

numberOfRowsInSection (it works good when I load my tableView (count=0) and after adding objects)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    switch (section) {
        case 2:
            return [[[self.fetchedResultsController sections] objectAtIndex:0] numberOfObjects];
        default:
            return 1;
    }
}

controllerWillChangeContent

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

controller didChangeObject

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;
    NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section -2 )];

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:@[frcIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:frcIndexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

controllerDidChangeContent

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

crash

Stack trace

Stack trace : (
0   Crossfit                            0x000000010267c40d -[MainSceneTableViewController controllerDidChangeContent:] + 61
1   CoreData                            0x000000010312cd08 __77-[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]_block_invoke + 4632
2   CoreData                            0x000000010303e937 developerSubmittedBlockToNSManagedObjectContextPerform + 199
3   CoreData                            0x000000010303e7fe -[NSManagedObjectContext performBlockAndWait:] + 222
4   CoreData                            0x000000010312bad7 -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 119
5   CoreFoundation                      0x000000010347977c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
6   CoreFoundation                      0x00000001034794bb _CFXRegistrationPost + 427
7   CoreFoundation                      0x0000000103479222 ___CFXNotificationPost_block_invoke + 50
8   CoreFoundation                      0x00000001034bea52 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1986
9   CoreFoundation                      0x0000000103374859 _CFXNotificationPost + 633
10  Foundation                          0x000000010272d1e9 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
11  CoreData                            0x0000000103024736 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 70
12  CoreData                            0x00000001030b507e -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 334
13  CoreData                            0x000000010301f569 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2809
14  CoreData                            0x0000000102ff7d44 -[NSManagedObjectContext executeFetchRequest:error:] + 436
15  Crossfit                            0x000000010267793b -[AKDataManager allObjects] + 283
16  Crossfit                            0x000000010267800b -[AKDataManager printAllObjects] + 43
17  Crossfit                            0x000000010267be6f -[MainSceneTableViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 495
18  UIKit                               0x00000001039b0b75 -[UITableView animateDeletionOfRowWithCell:] + 177
19  UIKit                               0x00000001039870dd __52-[UITableView _swipeActionButtonsForRowAtIndexPath:]_block_invoke + 80
20  UIKit                               0x00000001039b254e -[UITableView _actionButton:pushedInCell:] + 172
21  UIKit                               0x0000000103bd0376 -[UITableViewCell _actionButtonPushed:] + 82
22  UIKit                               0x000000010385ae91 -[UIApplication sendAction:to:from:forEvent:] + 92
23  UIKit                               0x00000001039c64d8 -[UIControl sendAction:to:forEvent:] + 67
24  UIKit                               0x00000001039c67a4 -[UIControl _sendActionsForEvents:withEvent:] + 311
25  UIKit                               0x00000001039c58d4 -[UIControl touchesEnded:withEvent:] + 601
26  UIKit                               0x0000000103d2c9ba _UIGestureRecognizerUpdate + 10279
27  UIKit                               0x00000001038c89c0 -[UIWindow _sendGesturesForEvent:] + 1137
28  UIKit                               0x00000001038c9bf6 -[UIWindow sendEvent:] + 849
29  UIKit                               0x00000001038792fa -[UIApplication sendEvent:] + 263
30  UIKit                               0x0000000103853abf _UIApplicationHandleEventQueue + 6844
31  CoreFoundation                      0x00000001033d9011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
32  CoreFoundation                      0x00000001033cef3c __CFRunLoopDoSources0 + 556
33  CoreFoundation                      0x00000001033ce3f3 __CFRunLoopRun + 867
34  CoreFoundation                      0x00000001033cde08 CFRunLoopRunSpecific + 488
35  GraphicsServices                    0x0000000106c68ad2 GSEventRunModal + 161
36  UIKit                               0x000000010385930d UIApplicationMain + 171
37  Crossfit                            0x0000000102683cbf main + 111
38  libdyld.dylib                       0x0000000105bd092d start + 1
39  ???                                 0x0000000000000003 0x0 + 3

)

Code for deleting

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

        NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section - 2)];
        self.index=frcIndexPath;

        AKDataManager* manager=[AKDataManager sharedManager];
        [manager printAllObjects];

        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:frcIndexPath]];



        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}
1
What is the crash message?Paulw11
Can you show your code where you are deleting a row please. ThanksFogmeister
@trojanfoe it's just stops with thread1 exc_bad_access (code1, adress = 0x8) I Just add stacktrace before [self.tableView endUpdates];Anton
@Anton please can you show the code where you are deleting stuff. Nothing here is deleting any objects from the table. What is triggering the update methods?Fogmeister

1 Answers

0
votes

You're inserting the index paths from the NSFetchedResultsController, but have only one section (2) that 'belongs' to it. Yet, it will probably try to update section 0.

To be more clear: In

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

you are subtracting two from the passed in section from the FRC (0), which makes -2. Try adding 2, this might fix is.