Question

Normally if I reload the table it does not crash. But when I get some data in background and then reload table to display that data and at same time if user is scrolling the table then app crashes. The reason is array of objects chatData is empty. I don't get how it is empty. Because just before reloading the table I set object into chatData. Note that it crashes only if user is scrolling at the same time.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Here app crashes when chatData is empty. Don't get why it is ever empty, because reloadData is called only after setting objects.
    if ([user.userId isEqualToString:[[chatData objectAtIndex:row] objectForKey:SET_SENDER]])
    {

    }
}

- (void)refreshTable
{
    .
    .
    .
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
    {
        self.chatData = [objects mutableCopy];
        [chatTable reloadData];
    }
}
Was it helpful?

Solution 2

Issue was that I was emptying the chatData somewhere in code and after that if table is reloaded then [chatData objectAtIndex:row] would cause app crash.

OTHER TIPS

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
    {
        self.chatData = [objects mutableCopy];
        [chatTable reloadData];
    }];

I assume this is doing its work on a background thread?

If so, you should move the assignment of the chatData and the reloadData call to the main thread, using dispatch_async, as any UI calls and any data that the UI touches should be done and assigned on the main thread.

Like so:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.chatData = [objects mutableCopy];
            [chatTable reloadData];
        });            
    }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top