Challenge - 5 Problems
URLSession Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you call resume() on a URLSessionDataTask?
You create a URLSessionDataTask but forget to call resume(). What will happen?
Attempts:
2 left
💡 Hint
Think about what resume() does for a URLSessionDataTask.
✗ Incorrect
URLSessionDataTask starts in a suspended state. You must call resume() to start it. Without resume(), the task stays suspended and does nothing.
🧠 Conceptual
intermediate2:00remaining
What is the role of the completion handler in URLSession dataTask?
Why do we provide a completion handler when creating a dataTask with URLSession?
Attempts:
2 left
💡 Hint
Think about what happens after the network call completes.
✗ Incorrect
The completion handler runs after the network request finishes. It provides the data received, the server response, and any error that occurred.
📝 Syntax
advanced2:00remaining
Which code snippet correctly creates and starts a URLSession data task?
Select the code that correctly creates a data task to fetch data from a URL and starts it.
iOS Swift
let url = URL(string: "https://example.com")! let task = URLSession.shared.dataTask(with: url) { data, response, error in // handle response } // What is missing here?
Attempts:
2 left
💡 Hint
Look for the method that starts the task.
✗ Incorrect
The correct method to start a URLSessionDataTask is resume(). Other methods do not exist and cause errors.
❓ lifecycle
advanced2:00remaining
What happens if you call resume() twice on the same URLSessionDataTask?
Consider this code:
let task = URLSession.shared.dataTask(with: url) { data, response, error in
// handle response
}
task.resume()
task.resume()
What is the effect of calling resume() twice?
Attempts:
2 left
💡 Hint
Think about the task state after the first resume().
✗ Incorrect
Calling resume() on a running task does nothing. The task runs once and ignores subsequent resume() calls.
🔧 Debug
expert2:00remaining
Why does this URLSession dataTask completion handler never run?
Look at this code:
let url = URL(string: "https://example.com")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
print("Response received")
}
// Missing line here
Why does "Response received" never print?
Attempts:
2 left
💡 Hint
Check if the task is started after creation.
✗ Incorrect
URLSessionDataTask starts suspended. Without calling resume(), the completion handler never runs.