ios - Breaking retain cycle with strong/weak self -


i've read posts strong/weak self break retain cycles still confused how work. understand use of __weak typeof(self) weakself = self create weak reference self confused strong reference. understand it, strong reference there strong reference self doesn't deallocated before end of block right? why necessary have __strong typeof(self) strongself = weakself? doesn't end pointing self object anyway? why not strongself = self?

any non-weak object reference inside block result in implicit retain on object, block being created. not executed, created.

if initialised inner strongself directly self, retain value of self , potentially cause retain cycle.

on other hand, if initialise weakself, not retain value of weakself.

this reason two-step. outer code copies value of self weakself, arc not add retain because __weak().

the block "creation" copies value of weakself (or @ least, manages makes value available @ execution time). can't see copied to, did.

at block "execution" time, block copies "value of weakself" (which nil if self has been dealloc'ed in mean time) strongself arc applies retain to. thus, duration of block, object referenced strongself remain alive, if alive begin with. if had relied on weakself, go nil @ time during execution of block.

note weak/strong pattern belt-and-braces - many examples rely on fact weakself go nil, , block silently become collection of no-ops (messages nil).

retain cycles typically occur if (a) keep reference block in self.property or (b) hand block off other object (notification manager, etc), , tell other object forget in dealloc; in both cases dealloc never called while block alive.

when people "the way stuff weak/strong pattern", assuming worst possible scenario.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -