-1

In order to fluently download and display images in my UITableViewCell, I have implemented the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *thisCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (_movieCell == nil) {
        thisCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }


    Movie* thisMovie = [_movies objectAtIndex:indexPath.row];
    thisCell.imageView.image = nil;
    thisCell.textLabel.text =thisMovie.Title;
    thisCell.imageView.image=[UIImage imageNamed:@"Dummy"];
    thisCell.detailTextLabel.text = thisMovie.Year;



    NSURL * imageURL = [NSURL URLWithString:[thisMovie.Poster stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];


    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:imageURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {

            UIImage *image = [UIImage imageWithData:data];
            if (image) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
                    if (updateCell)
                        updateCell.imageView.image = image;
                    //[updateCell setNeedsLayout];

                });
            }
        }

    }];
    [task resume];

    return thisCell;
}

That all works very well, except for the fact, that after a few second of at least the execution of the visible part of the code, the debug console tells me

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread.

This is part of a really simple app, that doesn't have any other background stuff going on, so I think it is this part of the code that is causing this issue.

Can anyone show me the mistake in my code, or, alternatively, explain a way to debug/track background threads?

Thanks ahead!

Sjakelien
  • 2,255
  • 3
  • 25
  • 43

1 Answers1

0

So, I should have clicked on the Related questions. The first Related gives a good way of debugging.

Community
  • 1
  • 1
Sjakelien
  • 2,255
  • 3
  • 25
  • 43