[英]weakSelf (the good), strongSelf (the bad) and blocks (the ugly)
I have read that when a block like this is executed: 我读过当执行这样的代码块时:
__weak typeof(self) weakSelf = self;
[self doSomethingInBackgroundWithBlock:^{
[weakSelf doSomethingInBlock];
// weakSelf could possibly be nil before reaching this point
[weakSelf doSomethingElseInBlock];
}];
it should be done this way: 应该这样:
__weak typeof(self) weakSelf = self;
[self doSomethingInBackgroundWithBlock:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf doSomethingInBlock];
[strongSelf doSomethingElseInBlock];
}
}];
So I want to replicate a situation where weakSelf gets nil in the middle of a block execution. 所以我想复制一个情况,在块执行过程中,weakSelf变为零。
So I have created the following code: 因此,我创建了以下代码:
* ViewController * * ViewController *
@interface ViewController ()
@property (strong, nonatomic) MyBlockContainer* blockContainer;
@end
@implementation ViewController
- (IBAction)caseB:(id)sender {
self.blockContainer = [[MyBlockContainer alloc] init];
[self.blockContainer createBlockWeakyfy];
[self performBlock];
}
- (IBAction)caseC:(id)sender {
self.blockContainer = [[MyBlockContainer alloc] init];
[self.blockContainer createBlockStrongify];
[self performBlock];
}
- (void) performBlock{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.blockContainer.myBlock();
});
[NSThread sleepForTimeInterval:1.0f];
self.blockContainer = nil;
NSLog(@"Block container reference set to nil");
}
@end
* MyBlockContainer * * MyBlockContainer *
@interface MyBlockContainer : NSObject
@property (strong) void(^myBlock)();
- (void) createBlockWeakyfy;
- (void) createBlockStrongify;
@end
@implementation MyBlockContainer
- (void) dealloc{
NSLog(@"Block Container Ey I have been dealloc!");
}
- (void) createBlockWeakyfy{
__weak __typeof__(self) weakSelf = self;
[self setMyBlock:^() {
[weakSelf sayHello];
[NSThread sleepForTimeInterval:5.0f];
[weakSelf sayGoodbye];
}];
}
- (void) createBlockStrongify{
__weak __typeof__(self) weakSelf = self;
[self setMyBlock:^() {
__typeof__(self) strongSelf = weakSelf;
if ( strongSelf ){
[strongSelf sayHello];
[NSThread sleepForTimeInterval:5.0f];
[strongSelf sayGoodbye];
}
}];
}
- (void) sayHello{
NSLog(@"HELLO!!!");
}
- (void) sayGoodbye{
NSLog(@"BYE!!!");
}
@end
So I was expecting that createBlockWeakyfy will generate the scenario I wanted to replicate but I haven't manage to do it. 因此,我期望createBlockWeakyfy会生成我想复制的场景,但是我没有做到。
The output is the same for createBlockWeakyfy and createBlockStrongify 输出与createBlockWeakyfy和createBlockStrongify相同
HELLO!!!
Block container reference set to nil
BYE!!!
Block Container Ey I have been dealloc!
Someone can tell me what I am doing wrong? 有人可以告诉我我做错了吗?
Your dispatch_async
block is a creating a strong reference. 您的dispatch_async
块是一个强大的参考。 When that block accesses your MyBlockContainer
to get its myBlock
property, it creates a strong reference to it for the life of that block. 当该块访问您的MyBlockContainer
获取其myBlock
属性,它创建了一个有力的参考,以它为块的使用寿命。
If you change your code to this: 如果将代码更改为此:
__weak void (^block)() = self.blockContainer.myBlock;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
block();
});
You should see the results you are expecting. 您应该看到预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.