弱自我可以在区块中间变成零吗? [英] Can __weak self turn nil in the middle of the block?
本文介绍了弱自我可以在区块中间变成零吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在运行于后台线程的代码块中使用__weak
self
引用时,我是否仅需要在开始时检查nil
,或者即使在执行过程中__weak
self
也可以变为nil第一个nil
测试通过后执行?我想从块中的self
访问一些ivars,并且在块执行时需要最新的值.
When using a __weak
self
reference in my block that runs on a background thread, do I only need to check for nil
in the beginning, or can the __weak
self
become nil even during execution after the first nil
test has passed? I want to access some ivars from self
in the block and I need the latest values at the time the block is executing.
推荐答案
如果没有人持有对self
的引用,那么可以.您可以通过在代码块中充分引用来缓解这种情况
If no one is holding a reference to self
then yes. You can mitigate this by taking a strong reference in the block
__weak __typeof(self) weakSelf = self;
^{
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
};
这篇关于弱自我可以在区块中间变成零吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文