Bird
0
0

What is wrong with this code snippet for updating a UIImageView after downloading an image?

medium📝 Debug Q7 of 15
iOS Swift - Networking
What is wrong with this code snippet for updating a UIImageView after downloading an image? URLSession.shared.dataTask(with: url) { data, response, error in if let data = data { let image = UIImage(data: data) imageView.image = image } }.resume()
AUI update is done on a background thread, which is unsafe
BUIImage cannot be created from data
CURLSession.shared.dataTask does not download data
DThe closure is missing a return statement
Step-by-Step Solution
Solution:
  1. Step 1: Identify thread context of closure

    Completion handler runs on background thread, not main thread.
  2. Step 2: Recognize UI update rules

    UI updates must be dispatched to main thread to avoid issues.
  3. Final Answer:

    UI update is done on a background thread, which is unsafe -> Option A
  4. Quick Check:

    UI updates require main thread [OK]
Quick Trick: Dispatch UI changes to main thread inside URLSession closures [OK]
Common Mistakes:
  • Updating UI directly in background thread
  • Assuming UIImage creation fails
  • Confusing closure return requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes