nonopolarity
nonopolarity

Reputation: 150976

Using iOS's UITableView, what if the cell is already dequeued before an image is downloaded into it?

No matter what method is used: iOS 5's [NSURLConnection sendAsynchronousRequest], SDWebImage, or ASIHTTPRequest, what if, while the image is being downloaded, the cell aleady got dequeued (when dequeueReusableCellWithIdentifier is called) and therefore, the new label may now be labeled as “Mary”, while Peter’s image is being downloaded, and when the download is completed, Peter's image is populated into Mary’s box.

It will be better if Mary's image is also being downloaded afterwards and will go into Mary's cell and cover up Peter's photo, but what if

1) Mary's photo takes 1 or 2 seconds to download (cell phone wireless network slow or delay), or the network just choke at that point? Then Peter's photo will show next to the name Mary for 1 or 2 seconds or 20 seconds, or even forever if the network request just failed this time.

2) It will be worst if Mary's data has a privacy setting of "don't show my photo" flag or something, then cellForRowAtIndexPath may actually set a dummy image (dummy avatar) into Mary's imageView, and when Peter's image is downloaded, go into Mary's cell and never get replaced.

Can this be elegantly handled or solved?

By the way, if we use SDWebImage or ASIHTTPRequest, will the issue will there or will it be handled by the class already?

Upvotes: 4

Views: 478

Answers (1)

Sixten Otto
Sixten Otto

Reputation: 14816

The UITableViewCell method -prepareForReuse is a great opportunity to cancel outstanding network requests, throw away unneeded caches, and that kind of thing.

+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:] doesn't directly afford a way to cancel the request, but if you set some property or ivar before you begin the request, and then check in the completion handler to be sure that its current value matches what you expect, you can get a similar effect.

Upvotes: 2

Related Questions