简体   繁体   中英

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

__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

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

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. If self is released before the block is executed, it will be nil within the block.

If the block should not do anything when self has already been deallocated, there is no reason to capture self strongly. Simply test for nil and exit early. Or do whatever work is needed in the block that doesn't act on 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.

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.

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 .

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. Because of retain cycle.

vc own apiHandler , apiHandler own successCallback and successCallback own vc .

So, using weak vc in block can avoid retain cycle.

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 .

When apiHandler do query success in background thread and invoke successCallback , the message doSomething will send to nil object. (Zombie)

This is why you need to use strongSelf in block like you said.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM