I always using [weak self]
in swift closure to prevent reference cycle.
Here is the code below, is it the correct way?
someTask(completion: {[weak self] (result) in
if self == nil {
return
}
//is it safe when reach here?
self!.xxx = yyy
self!.doLongTermWork()
self!.finish() //will crash when self is nil?
})
Weak self does not keep a strong hold on the instance. So when self.doLongTermWork()
, will self
be set to nil
again somewhere else?