2

This sample app will crash after some time if you constantly rotate the device during the row animations. My real app crashes sometime even on the first rotate during the row animations.

How should I protect my app from crashing during rotation with simultanous row animations? Please don't suggest to forbid rotation until the animations are done. DataSource is dependent on network fetches which may take anything from 1 to 30 seconds depending on user network and user wants to rotate the device if he sees the app is better viewed in landscape for example right after launch.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{

        for (int i = 0; i < 30; i++) {

            [NSThread sleepForTimeInterval:0.2]; // imitates fetching and parsing
            [self.array addObject:[NSString stringWithFormat:@"cell number %d", i]];

            dispatch_async(dispatch_get_main_queue(), ^{

                // perform on main
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            });
        }
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = self.array[indexPath.row];

    return cell;
}

- (NSMutableArray *)array
{
    if (!_array) {
        _array = [[NSMutableArray alloc] init];
    }
    return _array;
}

Crash report

2014-02-21 12:47:24.667 RowsAnimationRotate[2062:60b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:1330
2014-02-21 12:47:24.673 RowsAnimationRotate[2062:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (8) must be equal to the number of rows contained in that section before the update (8), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Martin Koles
  • 5,177
  • 8
  • 39
  • 59
  • Not sure if device rotation affect on it – Injectios Feb 21 '14 at 12:16
  • 1
    See my answer regarding race condition. The rotation is a red herring, but is probably increasing the likelihood of it happening due to altering the timing of stuff happening on the two threads. – Mike Pollard Feb 21 '14 at 12:28

3 Answers3

9

You have essentially created a race condition.

The problem is you are manipulating self.array in a background thread while self.tableView insertRowsAtIndexPaths is running on the main thread and will be accessing self.array.

So at some pointself.tableView insertRowsAtIndexPaths (or other tableView methods called as a result of this) is running on the main thread, expecting a certain number of objects in self.array, but the background thread gets in there and adds another one...

To fix your simulation:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{

    for (int i = 0; i < 30; i++) {

        [NSThread sleepForTimeInterval:0.2]; // imitates fetching and parsing
        NSString *myNewObject = [NSString stringWithFormat:@"cell number %d", i]];

        dispatch_async(dispatch_get_main_queue(), ^{

            // perform on main
            [self.array addObject: myNewObject];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        });
    }
});
Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
0

can you change

dispatch_async(dispatch_get_main_queue(), ^{

                // perform on main
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            });

with:

dispatch_async(dispatch_get_main_queue(), ^{

                // perform on main
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            });
chawki
  • 867
  • 1
  • 8
  • 13
0

Can you please try your simulation with synchronized:-

It declares a critical section around the code block. In multithreaded code, @synchronized guarantees that only one thread can be executing that code in the block at any given time.

@synchronized(self.tableView) {
 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{

        for (int i = 0; i < 30; i++) {

            [NSThread sleepForTimeInterval:0.2]; // imitates fetching and parsing
            [self.array addObject:[NSString stringWithFormat:@"cell number %d", i]];

            dispatch_async(dispatch_get_main_queue(), ^{

                // perform on main
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            });
        }
    });                                    
                                }

Hope that helps. Please let me know if we have to go with another solution.

  • I did not know about @synchronized. But the app still crashes with your code. Mike Pollard's advice actually works. – Martin Koles Feb 21 '14 at 13:01
  • From what I understand about @synchronized and locks this is NOT a solution. Refer to [this answer](http://stackoverflow.com/questions/1215330/how-does-synchronized-lock-unlock-in-objective-c) – Mike Pollard Feb 21 '14 at 14:50