Using iOS UITableView, what if a cell is already unloaded before the image is loaded into it?

Regardless of which method is used: iOS 5 [NSURLConnection sendAsynchronousRequest] , SDWebImage or ASIHTTPRequest , that if, while the image is loading, the aleady cell has been deleted (when dequeueReusableCellWithIdentifier is dequeueReusableCellWithIdentifier ), and therefore, the new shortcut can now be marked as “Mary” while the Peters image is loading, and when the download is complete, the image of Peter is populated in the Marys field.

It would be better if Mary’s image is also uploaded later and goes to Mary’s cell and covers Peter’s photo, but what if

1) Does Mary’s photo take 1 or 2 seconds to download (slow or cellular phone network delay), or is the network just choking at this point? Then the photo of Peter will be displayed 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 worse if Mary’s data has a privacy setting of "Do not show my photo" or something else, then cellForRowAtIndexPath can actually set the dummy image (dummy avatar) in Mary imageView and when the image of Peter loads, go to Mary’s cell and never replace.

Could this be elegantly crafted or resolved?

By the way, if we use SDWebImage or ASIHTTPRequest , will the problem be there or will the class handle it already?

+4
source share
1 answer

UITableViewCell method -prepareForReuse - a great opportunity to cancel outstanding network requests, throw out unnecessary caches and similar things.

+[NSURLConnection sendAsynchronousRequest:queue:completionHandler:] does not allow you to directly cancel the request, but if you set any property or ivar before starting the request, then check the completion handler to make sure that its current value matches the expected one, you can get a similar Effect.

+2
source

Source: https://habr.com/ru/post/1432388/


All Articles