0
0
iOS Swiftmobile~20 mins

Error handling for network calls in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Network Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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?
    }
  }
}
ADispatchQueue.global().async { self.errorLabel.text = error.localizedDescription }
BDispatchQueue.main.async { self.errorLabel.text = error.localizedDescription }
Cprint(error.localizedDescription)
Dself.errorLabel.text = error.localizedDescription
Attempts:
2 left
💡 Hint
UI updates must happen on the main thread.
🧠 Conceptual
intermediate
1:30remaining
Understanding URLSession error handling
What does the 'error' parameter in URLSession dataTask completion handler represent?
AThe HTTP status code of the response
BThe data returned from the server
CA client-side error such as network failure or request timeout
DAn error returned by the server in the HTTP response body
Attempts:
2 left
💡 Hint
Think about what kind of errors happen before getting a response.
lifecycle
advanced
2: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?
ADo nothing; the system cancels tasks automatically
BSet task to nil inside viewDidDisappear method
CCall task.resume() inside viewWillDisappear method
DCall task.cancel() inside viewWillDisappear method
Attempts:
2 left
💡 Hint
Think about when you want to stop the network call cleanly.
📝 Syntax
advanced
2: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)
  }
}
Ado { let data = try await fetchData(); process(data) } catch { handle(error) }
Blet data = await fetchData() catch { handle(error) }
Ctry { let data = await fetchData() } catch { handle(error) }
Dlet data = try fetchData() await process(data)
Attempts:
2 left
💡 Hint
Remember the correct placement of try, await, and do-catch blocks.
🔧 Debug
expert
3: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()
AThe errorLabel update is outside the error check, so it always shows failure even if no error
BThe UI update runs regardless of error presence, so errorLabel always shows failure even on success
CThe error check is incorrect; it should be if error == nil
DThe network call is missing .resume() to start the task
Attempts:
2 left
💡 Hint
Look where the UI update code is placed relative to the error check.