Bird
0
0

What is wrong with this async GET request code snippet?

medium📝 Debug Q7 of 15
iOS Swift - Networking
What is wrong with this async GET request code snippet?
func fetchData() async throws {
  let url = URL(string: "https://api.com")!
  let (data, _) = try await URLSession.shared.data(from: url)
  print(String(data: data, encoding: .utf8)!)
}

Task {
  try fetchData()
}
AURL is force unwrapped unsafely
BMissing await when calling fetchData() inside Task
CString conversion from data is unsafe
DTask closure does not handle errors
Step-by-Step Solution
Solution:
  1. Step 1: Analyze Task call

    fetchData() is async and throws, so calling it requires await and try.
  2. Step 2: Identify missing await

    The code calls try fetchData() without await, causing a compile error.
  3. Final Answer:

    Missing await when calling fetchData() inside Task -> Option B
  4. Quick Check:

    Async call needs await = A [OK]
Quick Trick: Use await when calling async functions inside Task [OK]
Common Mistakes:
  • Omitting await with async calls
  • Ignoring error handling in Task
  • Force unwrapping URL without checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes