简体   繁体   English

当weakSelf在块中为零时,应该添加strongSelf

[英]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 . 例如,您有一个名为vcUIViewController实例, 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拥有apiHandlerapiHandler拥有自己的successCallbacksuccessCallback自己的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.

相关问题 我是否需要在代码块中检查strongSelf = weakSelf分配是否为零? - Do I need to check for nil on my strongSelf = weakSelf assignment inside a block? 我什么时候应该在块中使用弱势,为什么砌体中没有保留周期? - When should I use weakself in a block, and why there is no retain cycle in Masonry? 在从 UIViewController 调用的非保留完成中引用 self 时,weakSelf/strongSelf 舞蹈真的有必要吗? - Is the weakSelf/strongSelf dance really necessary when referencing self inside a non-retained completion called from a UIViewController? 设置框架时为弱 - weakSelf when setting frame iOS - 我应该在这个区块中调用“weakSelf”吗? - iOS - Should I be calling “weakSelf” in this block? 弱自我(好),强自我(坏)和阻止(丑陋) - weakSelf (the good), strongSelf (the bad) and blocks (the ugly) 在ARC中,dealloc方法调用包含对self的弱引用的method / block,从而导致weakSelf = nil - In ARC, dealloc method calls method/block that contains weak reference to self result in weakSelf = nil 执行处理程序块时,UIAlertController为nil - UIAlertController is nil when handler block executes 我什么时候应该将可选值与nil进行比较? - When should I compare an optional value to nil? 应该配置自定义CollectionViewCell的ImageView为零 - ImageView of a custom CollectionViewCell is nil when it should be configured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM