The only reason to capture self
, as a weak or strong reference, in a block is because the block uses it in some way.
When self
has been captured weakly how it is used will determine when, and if, you need to make a strong copy first.
If the block is going to use the captured reference multiple times then a strong copy should always be made, this insures the reference stays alive across all the uses within the block and avoids the cost of loading a weak reference multiple times.
For example, if the block's function is dependent on whether self
still exists then it would be usual to start by making a strong reference and testing it. Something along the lines of:
__weak __typeof(self)weakSelf = self;
myBlock = ^{
// make local strong reference to self
__typeof(weakSelf) strongSelf = weakSelf;
// check if self still exists and process accordingly
if (strongSelf)
{
// do whatever is needed if "self" still exists
// strongSelf will keep the object alive for the
// duration of the call
}
else
{
// do whatever, if anything, is needed if "self" no longer exists
}
});
However if the block's operation only optionally requires the use of the object referenced by self
then a strong copy may never be made.
HTH