Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tableView crashes on end up with more than 16 items

This is very confusing.

I have a UITableView, which updates and works fine until it gets more than 16 items then it crashes when trying to endUpdates after calling insertRowsAtIndexPaths.

The NSIndexPaths being added are all valid. -numberOfRowsInSection returns the correct number. It is not throwing an error related to the data set, rather it crashes with

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

when endUpdates is called on the tableView.

The data source is all there, the NSIndexPaths are fine. the code works fine between 0 and 16 rows, but when I add a 17th it crashes. Additionally if I start it with 22 items it works fine, when I add the 23rd it crashes... if I call reload data instead of doing the update and insert process it works fine, so it's nothing to do with the data itself, and it shouldn't be anything to do with how I'm inserting the rows since it works through 16...

I'm completely perplexed. Here is my update method. It is being called on the main thread at all times.

- (void)updateConversation:(NSNotification*)notification
{
    NSDictionary *updateInfo = [notification userInfo];
    //NSLog(@"Got update %@", updateInfo);

    if ([[updateInfo objectForKey:@"success"] integerValue] == YES) {
        [self updateConversationUI];

        int addedStatementCount = [[updateInfo objectForKey:@"addedStatementCount"] intValue];

        if (addedStatementCount > 0) {
            //[self.tableView reloadData];
            [self.tableView beginUpdates];
            int previousStatmentCount = [[updateInfo objectForKey:@"previousStatmentCount"] intValue];

            NSLog(@"owner %i, Was %i, now %i, change of %i", self.owner, previousStatmentCount, (int)self.conversation.statements.count, addedStatementCount);

            NSMutableArray *rowPaths = [[NSMutableArray alloc] init];

            for (int i = previousStatmentCount; i < previousStatmentCount + addedStatementCount; i++) {
                NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
                [rowPaths addObject:path];
            }

            [self.tableView insertRowsAtIndexPaths:rowPaths withRowAnimation:UITableViewRowAnimationBottom];        
            [self.tableView endUpdates];
            [self.tableView scrollToRowAtIndexPath:[rowPaths lastObject] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }
    }
}

The rest of the crash past [self.tableView endUpdates] is UITableView

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010189b795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001015fe991 objc_exception_throw + 43
    2   CoreFoundation                      0x0000000101852564 -[__NSArrayM insertObject:atIndex:] + 820
    3   UIKit                               0x0000000100317900 __46-[UITableView _updateWithItems:updateSupport:]_block_invoke691 + 173
    4   UIKit                               0x00000001002b5daf +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 460
    5   UIKit                               0x00000001002b6004 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 57
    6   UIKit                               0x00000001003174cb -[UITableView _updateWithItems:updateSupport:] + 2632
    7   UIKit                               0x0000000100312b18 -[UITableView _endCellAnimationsWithContext:] + 11615
    8   Dev App                         0x0000000100006036 -[ConversationViewController updateConversation:] + 998
    9   CoreFoundation                      0x00000001018f121c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    10  CoreFoundation                      0x000000010185370d _CFXNotificationPost + 2381
    11  Dev App                         0x00000001000055ac -[ConversationManager postNotification:] + 92
    12  Foundation                          0x0000000101204557 __NSThreadPerformPerform + 227
    13  CoreFoundation                      0x000000010182aec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x000000010182a792 __CFRunLoopDoSources0 + 242
    15  CoreFoundation                      0x000000010184661f __CFRunLoopRun + 767
    16  CoreFoundation                      0x0000000101845f33 CFRunLoopRunSpecific + 467
    17  GraphicsServices                    0x00000001039a23a0 GSEventRunModal + 161
    18  UIKit                               0x0000000100261043 UIApplicationMain + 1010
    19  Dev App                         0x0000000100003613 main + 115
    20  libdyld.dylib                       0x0000000101f2a5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

This seems like a bug in the OS, the stack would indicate that it's something to do with the animation block but it happens no matter what I set the animation to, including none.

And to re-state. This works through 16 items, then inserting items past that causes a crash. It is also called at initial setup to load data, and it will load any number of items there, including well over 16. But when called again, purely as an update from 16 or more to something higher it will crash.

Most bizarre issue I've yet to encounter with table views...

under iOS 7 on device and simulator, latest Xcode for reference.

like image 500
ima747 Avatar asked Dec 15 '13 14:12

ima747


1 Answers

It seems that the problem is caused by my call of scrollToRowAtIndexPath (even though it crashes before it gets there...) combined with implementing tableView:estimatedHeightForRowAtIndexPath: by removing the row height estimate the crash went away... still seems like a bug in the animation system for tables to me. Thankfully I don't need the estimated row height, I had forgotten I had implemented it (trying to play nice by iOS 7 bites me again).

like image 173
ima747 Avatar answered Nov 15 '22 20:11

ima747