Daily iOS
UIKit

Image cell reuse and cancellation

In a UIKit collection view with image cells, fast scrolling makes the previous request's response flash on a reused cell before it changes. What would you cancel when the cell is reused?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

The cell keeps its in-flight image load Task in a property, and in prepareForReuse() it cancels that Task and clears the image view. Then configure starts a new Task for the new URL, and inside the Task the image is assigned only after checking for cancellation, so a late response never lands on the wrong cell.

Share a question