objective c - Will this hold self inside a block? -
ok, code on arc holds self inside block:
dispatch_async(someq, ^{ [self dosomething]; });
and can solved by
__weak myrequest *weakself = self; dispatch_async(someq, ^{ [weakself dosomething]; });
but if dosomething has lot of references self? because dosomething code run inside block isn't same of code of dosomething on block directly?
do need pass weakself dosomething method can use weakself references instead of self? like
__weak myrequest *weakself = self; dispatch_async(someq, ^{ [weakself dosomethingusingthisself:weakself]; });
by doing:
myrequest *__weak weakself = self; dispatch_async(someq, ^{ [weakself dosomething]; });
...when there no more references self
, weakself
automatically becomes nil
. async callback end sending dosomething
nil
in object destroyed before callback, nothing.
if weakself
not nil, it's pointer object. when send selector object, there 2 implicit arguments: self
, _cmd
(see documentation imp), section 6.1). weakself
becomes implicit self
in calls. @ point, self
strong reference, object won't destroyed out underneath during dosomething
call chain, referencing self
dosomething
doesn't cause reference count increment because block doesn't know goes on inside of dosomething
.
Comments
Post a Comment