Challenge - 5 Problems
Network Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Handling network error UI update
You make a network call that can fail. Which option correctly updates the UI to show an error message when the call fails?
iOS Swift
func fetchData() {
networkCall { result in
switch result {
case .success(let data):
self.updateUI(with: data)
case .failure(let error):
// What to do here?
}
}
}Attempts:
2 left
💡 Hint
UI updates must happen on the main thread.
✗ Incorrect
UI changes in iOS must be done on the main thread. Option B uses DispatchQueue.main.async to update the error label text safely. Option B updates UI on a background thread, which can cause issues. Option B updates UI on a global background thread, which is incorrect. Option B only prints the error and does not update the UI.
🧠 Conceptual
intermediate1:30remaining
Understanding URLSession error handling
What does the 'error' parameter in URLSession dataTask completion handler represent?
Attempts:
2 left
💡 Hint
Think about what kind of errors happen before getting a response.
✗ Incorrect
The 'error' parameter represents client-side errors like no internet connection or timeout. Server errors come as HTTP status codes in the response, not as this error parameter.
❓ lifecycle
advanced2:00remaining
Properly cancelling a network call on view disappear
You start a URLSession dataTask in a view controller. What is the best way to cancel the task when the view disappears to avoid memory leaks or unwanted callbacks?
Attempts:
2 left
💡 Hint
Think about when you want to stop the network call cleanly.
✗ Incorrect
Calling task.cancel() inside viewWillDisappear stops the network call and prevents callbacks after the view is gone. Setting task to nil does not cancel the task. The system does not cancel tasks automatically. Calling resume() starts or resumes the task, which is the opposite of cancel.
📝 Syntax
advanced2:30remaining
Correct error handling syntax in async network call
Which option correctly uses Swift's async/await syntax with error handling for a network call?
iOS Swift
func loadData() async { do { let data = try await fetchData() process(data) } catch { handle(error) } }
Attempts:
2 left
💡 Hint
Remember the correct placement of try, await, and do-catch blocks.
✗ Incorrect
Option A correctly wraps the try await call inside a do block and catches errors. Option A has invalid syntax mixing await and catch. Option A misuses try without await and wrong do-catch syntax. Option A misorders try and await and misses async context.
🔧 Debug
expert3:00remaining
Diagnosing error message on successful network calls
You notice your app shows an error message even when the network call succeeds. Which option is the most likely cause?
iOS Swift
URLSession.shared.dataTask(with: url) { data, response, error in if error != nil { print("Error occurred") } DispatchQueue.main.async { self.errorLabel.text = "Failed to load" } }.resume()
Attempts:
2 left
💡 Hint
Look where the UI update code is placed relative to the error check.
✗ Incorrect
The errorLabel.text update is outside the if error != nil block, so it runs always, showing failure even on success. This can confuse the user. The correct approach is to update UI only inside the error check. Option A describes the symptom but B pinpoints the cause. Option A is wrong logic. Option A is false because .resume() is present.