self refrence inside swift closure return nil some time

元气小坏坏 提交于 2019-12-04 19:48:14

The whole point of [weak self] is to not create a reference to self (probably to avoid circular links and memory leaks) so that it can be released. If that happens, then self will be nil.

You should either not use [weak self] or, better yet probably, be prepared to handle the case of self having been released and set to nil.

guard let strong = self else { return }

Take the example:

import UIKit
import PlaygroundSupport

class Foo {
    let name : String

    init(name:String) {
        self.name = name

        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
            print(self!.name)
        }
    }
}

Foo(name:"freddie")

PlaygroundPage.current.needsIndefiniteExecution = true

In this case, you'll get a crash, because self is released before the async callback is made.

You can either change the asyncAfter call to be:

        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            print(self.name)
        }

will guarantee that self isn't released until after the callback is made.

Or you can use something like:

        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
            guard let strong = self else { print("self released") ; return }
            print(strong.name)
        }

I am able to fix this issue by making strong instance while creating it, in my case this weak variable was making nil to self. Thanks all for providing suggestion for my queries.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!