Open peternlewis opened 7 years ago
Is sself
needed? Could we just use wself
directly?
e.g.
__weak __typeof(self) wself = self;
dispatch_async(_writeQueue, ^{
if (!wself) {
return;
}
NSOutputStream *outStream = wself.outputStream;
if (!outStream) {
return;
}
...
}
And SRProxyConnect
will not retain the block in _writeData
, there will not be any cycle reference. Therefore, is __weak
actually needed?
OK, I got it. If we don't assign a weak object to strong, there will be a warning:
weak variable 'wself' is accessed multiple times in this block but may be unpredictably set to nil; assign to a strong variable to keep the object alive [-Warc-repeated-use-of-weak]
Yes, you've worked through it all.
You are right that you could just use self
in the block if you're happy for the async block to keep the connection retained until it executes, rather than doing nothing if the connection is otherwise released, but probably the latter behaviour is better, thus the weak/strong dance.
Note that the variable wself is never actually used, except to get it's type "typeof(wself)".
The line should be: