Wednesday 18 July 2018

What causes outOfBounds error in cellForRowAtIndexPath

I'm having the following issue raised by Crashlytics :

[__NSArrayM objectAtIndexedSubscript:]: index 5 beyond bounds for empty array
-TopicListViewController tableView:cellForRowAtIndexPath:]

While accessing the dataSource with indexPath.row.

We have some asynchronous data update updating the datasource, and that variable is nonatomic.

Would it be possible that cellForRowAtIndexPath is called while the dataSource is being updated? Hence causing to access an index that doesn't exist anymore?

Can it be because the variable is nonatomic?

Here's the relevant code :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row > [self.tableData count] - 1 || ![self.tableData isValidArray]) {
        return nil; //Some protection to prevent this issue...
    }

    TopicCell * cell = (TopicCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.delegate = self;

    NSDictionary * data = nil;

    if (self.we_isSearching) {
        data = self.we_searchResult[indexPath.row];
    } else {
        data = [self.tableData objectAtIndex:indexPath.row]; //Crashes here
    }



from What causes outOfBounds error in cellForRowAtIndexPath

No comments:

Post a Comment