Not the complete answer, but the “improvement” of starting remote fetching in an asynchronous task:
// Trigger background task that retrieves the actual thumbnail from the URL. [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* imageData, NSError* error) { if (error) { [self handleError:error]; // connection failed return; } // here, at a minimum, check status code and Content-Type: // ... add code if (statusCode == 200 && contentTypeOK) // "OK" { [FTWCache setObject:imageData forKey:[MD5 hash:thumbnailURL]]; [tableView cellForRowAtIndexPath:indexPath].imageView.image = [UIImage imageWithData:imageData]; } else { // we didn't get an image, possibly other errors (eg authentication, etc.) NSError* err = ...; // add code [self handleError:err]; return; } }];
This code does not have the ability to cancel it, if necessary. There is also no means to prevent multiple requests from starting for the same image.
The aforementioned problems, together with a specific user scenario, can lead to an unlimited number of running network requests, which will ultimately lead to an application crash due to memory problems. To improve this, we need to use the NSURLConnection implementation of the asynchronous style with delegates, which gives us the opportunity to cancel the request and perform additional measurements that will not be caused by multiple requests.
Edit:
An additional improvement will be the insertion of image data into the cache, as well as the creation of a UIImage object in the background job, the completion handler of which updates the cell and executes it in the main thread.
source share