Spot the bug
Results from an old query sometimes replace newer ones, and work continues after the screen closes. Tap the line that causes it.
struct SearchView: View {let query: String@State private var results: [Item] = []var body: some View {List(results) { Text($0.title) }.onAppear {Task { results = await API.search(query) }}}}
Hint 1 of 1
Who cancels the Task on line 7?
Choose an answer, then open Show answer below to check it.
Show answer
- Answer: line 7
Task { results = await API.search(query) }
Line 7 starts an unstructured Task that is never cancelled and never re-runs for a new query. Use .task(id: query) { results = await API.search(query) }.