[英]when weakSelf will be nil in block ,when should add strongSelf
By using weakSelf in the block ,you are avoiding retain cycle.But sometime you should hold weakSelf until block retain ,therefor you need use strongSelf just like 通过在块中使用weakSelf,你可以避免保留循环。但有时你应该持有weakSelf直到阻塞,因此你需要使用strongSelf就像
__weak __typeof__(self) weakSelf = self;
dispatch_group_async(_operationsGroup, _operationsQueue, ^
{
__typeof__(self) strongSelf = weakSelf;
[strongSelf doSomething];
[strongSelf doSomethingElse];
} );
I want know when weakSelf will be nil ,then we should add __strong typeof(self) strongSelf = weakSelf
我想知道当weakSelf为零时,我们应该添加
__strong typeof(self) strongSelf = weakSelf
Example: If your aunty asked you, "Please purchase the umbrella from the market before I leave for my flight". 例如:如果你的阿姨问你,“请在我离开之前从市场上购买雨伞”。 You went to the market and it was very hard to find an umbrella.
你去了市场,很难找到一把雨伞。 Finally you found a nice umbrella after few hours and you reach home but you find out that your aunt has left and you feel bad.
最后你在几个小时后找到了一把漂亮的雨伞,然后你到家了,但是你发现你的姨妈已经离开了,你感觉很糟糕。 But that was the right thing for your aunt because the flight is more important than the umbrella.
但这对你的阿姨来说是正确的,因为飞行比伞更重要。
But in your coding problem what you're trying to do is You are visiting market and taking your aunt's passport with you so that she won't leave until you come back. 但是在您的编码问题中,您正在尝试做的是您正在访问市场并随身携带阿姨的护照,以便在您回来之前不会离开。
I guess that's rude, but if you still want to do that, use self
instead of strongSelf
我猜这很粗鲁,但如果你还想这样做,那就用
self
而不是strongSelf
dispatch_group_async(_operationsGroup, _operationsQueue, ^
{
[self doSomething];
[self doSomethingElse];
} );
It all depends on your requirements. 这一切都取决于您的要求。 There's no one, right answer.
没有一个正确的答案。
Capturing self
weakly allows the instance to be deallocated, even if the block is still retained somewhere. 弱
self
捕获允许实例被释放,即使块仍然保留在某处。 If self
is released before the block is executed, it will be nil
within the block. 如果在执行块之前释放
self
则块内将为nil
。
If the block should not do anything when self
has already been deallocated, there is no reason to capture self
strongly. 如果在
self
已经解除分配时块不应该做任何事情,那么就没有理由强烈地捕获self
。 Simply test for nil
and exit early. 只需测试
nil
并提前退出。 Or do whatever work is needed in the block that doesn't act on self
. 或做力所能及的工作,需要在不上作用块
self
。
If self
should not disappear until the block has executed, capture self
strongly, but avoid retain cycles by ensuring that self
does not have a strong reference to the block. 如果
self
在块执行之前不应消失,则强烈捕获self
,但通过确保self
没有对块的强引用来避免保留周期。
If it's OK for self
to disappear before the block begins executing, but must stick around until the block finishes, the block should capture a strong reference to the weakly-captured self
when it begins. 如果在块开始执行之前
self
可以消失,但必须坚持到块完成,该块应该在它开始时捕获对弱捕获self
的强引用。
If question is: "when weakSelf will be nil". 如果问题是:“当弱自己将是零时”。
The answer is: When no one own the instance of self 答案是:当没有人拥有自我的实例时
For example, you have a UIViewController
instance named vc
, vc
have a dependency for query API named apiHandler
, and api
have a callback block named successCallback
. 例如,您有一个名为
vc
的UIViewController
实例, vc
对名为apiHandler
查询API具有依赖关系,而api
具有名为successCallback
的回调块。
like this: 像这样:
@interface ApiHandler: NSObject
@property (nonatomic, copy) void (^successCallback)();
@end
@interface vc : UIViewController
@property (nonatomic, strong) ApiHandler *apiHandler;
@end
@implementation vc
- (void)doQuery {
self.apiHandler.successCallback = ^{
[self doSomething];
};
}
- (void)doSomething {
}
If vc
be pop or dismiss, the vc
instance WILL NOT BE dealloc. 如果
vc
是pop或dismiss,则vc
实例将不会被释放。 Because of retain cycle. 因为保留周期。
vc
own apiHandler
, apiHandler
own successCallback
and successCallback
own vc
. vc
拥有apiHandler
, apiHandler
拥有自己的successCallback
和successCallback
自己的vc
。
So, using weak vc
in block can avoid retain cycle. 因此,在块中使用弱
vc
可以避免保留周期。
like this: 像这样:
- (void)doQuery {
__weak __typeof__(self) weakSelf = self;
self.apiHandler.successCallback = ^{
[weakSelf doSomething];
};
}
And now, if your vc
be pop or dismiss, the vc
instance will be dealloc, and weakSelf will be nil . 现在,如果您的
vc
是pop或dismiss,则vc
实例将是dealloc,而weakSelf将为nil 。
When apiHandler
do query success in background thread and invoke successCallback
, the message doSomething
will send to nil
object. 当
apiHandler
在后台线程中查询成功并调用successCallback
,消息doSomething
将发送到nil
对象。 (Zombie) (僵尸)
This is why you need to use strongSelf in block like you said. 这就是为什么你需要像你说的那样在块中使用strongSelf。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.