Code example:
func someAsyncFunc() async {
loadingTask?.cancel()
return await withCheckedContinuation { [weak self] checkedContinuation in
guard let self else {
return
}
loadingTask = Task { [weak self] in
guard let self else {
return
}
//some code with self
checkedContinuation.resume()
}
}
}
I understand that one of [weak self] calls is extra - this example just shows 2 possible code chunks where it can be placed.
According to one documentation article withCheckedContinuation should always call checkedContinuation.resume(), according to another documentation article you should use [weak self] to avoid strong reference cycles. But how to use both of them simultaneously?
If I leave code AS-IS then checkedContinuation.resume() may not be called and it leads to memory leaks. If I don't use [weak self] it leads to retain cycles which lead to memory leaks too. I also can't just replace withCheckedContinuation with withCheckedThrowingContinuation because it raises code complexity and also means I should forget about withCheckedContinuation (because any similar method may contain [weak self]).
Taskin awithCheckedContinuation? How about justawaittheTaskdirectly?Taskthere may be another async func (for example, a func with completion block) but the problem with[weak self]persists.