There are already over 400 questions on StackOverflow with the Swift tag that relate to asynchronous execution. It feels like there has been a recent increase in the number of questions asked by low-rep users that all seem to misunderstand the same problem:
func getData() -> [String] {
var data: [String] = []
someAsyncFunc {
data = $0
print(data)
}
return data
}
// Why does `getData() == []` even though it isn't empty when I print!?
(If you would like to see more, just search for "[swift] asynchronous.") The point is that there are a lot of virtually identical questions: They all essentially deal with the above block of code; they all misunderstand that someAsyncFunc
does not block the calling thread; and they all have the same answer.
There was a similar post on this site about Ajax almost 9(!) years ago, which suggested picking a very good answer and marking these new questions as duplicates. We could do the same thing for Swift. However, I can't find a great answer that is also general enough. Alternatively, we could keep doing what we're doing: leave a comment and let someone else give a slightly specialized answer. What is the best policy?