1

I always like to use [weak self], to prevent any potential memory leakage.

I was wondering, is 2nd [weak self] required, in a closure inside another closure? For instance

2 [weak self]

func takePhoto() {
    AVCaptureDevice.requestAccess(for: AVMediaType.video) { [weak self] response in
        DispatchQueue.main.async { [weak self] in
            guard let self = self else { return }

Or, it is redundant to do so for the above case, and single [weak self] will be sufficient?

Single [weak self]

func takePhoto() {
    AVCaptureDevice.requestAccess(for: AVMediaType.video) { [weak self] response in
        DispatchQueue.main.async {
            guard let self = self else { return }

May I know, which one is the most accurate approach?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • Unrelated, but in fact, you shouldn't need a `weak self` with `DispatchQueue`, see https://stackoverflow.com/a/66542760/1801544 so let's imagine it's another methid with a closure which could retain... – Larme Aug 20 '21 at 16:35
  • Else, see https://stackoverflow.com/questions/42168527/in-swift-when-using-weak-self-in-should-i-double-up-on-it-when-nested-insi and other related questions. – Larme Aug 20 '21 at 16:36
  • 1
    Does this answer your question? [Where does the weak self go?](https://stackoverflow.com/questions/41991467/where-does-the-weak-self-go) – Phil Dukhov Aug 20 '21 at 16:44

0 Answers0