سؤال

I have a table view than is populated with items from an RSS feed. When a row is selected, the title, link, description, and image are passed to a detail view. This works but there is a noticeable delay between when the row is selected and when the detail view opens. I'm trying to find a way to optimize this so the delay is less noticeable.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GRSItemDetail *detail = [[GRSItemDetail alloc]initWithNibName:@"GRSItemDetail" bundle:nil];

    RSSItem *entry = [[channel items]objectAtIndex:[indexPath row]];

    NSURL *imageLink = [NSURL URLWithString:[entry bigImageURL]];
    NSData *data = [NSData dataWithContentsOfURL:imageLink];
    UIImage *image = [[UIImage alloc]initWithData:data];

    detail.titleString = [entry title];
    detail.descriptionString = [entry infoString];
    detail.urlString = [entry link];
    detail.itemImage = image;

    [self.navigationController pushViewController:detail animated:YES];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
هل كانت مفيدة؟

المحلول

The delay is caused by your use of NSData *data = [NSData dataWithContentsOfURL:imageLink];. This is synchronously downloading data from the web and blocking the main thread while that happens.

You should have the detail view controller handle an asynchronous download after it has been shown. Consider using a library like SDWebImage to make this easy. To enable this, change detail.itemImage to set the image URL rather than the image itself and then load the image as you configure the detail view.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top