문제

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];
    }
}
도움이 되었습니까?

해결책 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.

다른 팁

[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];
        });            
    }];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top