weakSelf(好的),strongSelf(不好的)和blocks(丑陋的)

8

我了解到当执行像这样的代码块时:

__weak typeof(self) weakSelf = self;
[self doSomethingInBackgroundWithBlock:^{
        [weakSelf doSomethingInBlock];
        // weakSelf could possibly be nil before reaching this point
        [weakSelf doSomethingElseInBlock];
}]; 

应该这样做:
__weak typeof(self) weakSelf = self;
[self doSomethingInBackgroundWithBlock:^{
    __strong typeof(weakSelf) strongSelf = weakSelf;
    if (strongSelf) {
        [strongSelf doSomethingInBlock];
        [strongSelf doSomethingElseInBlock];
    }
}];

我希望能够复制一种情况,即在块执行过程中weakSelf获取到nil的情况。

因此,我创建了以下代码:

* 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 *

@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

我以为createBlockWeakyfy会生成我想要复制的场景,但我还没有成功。

createBlockWeakyfy和createBlockStrongify的输出结果是相同的。

HELLO!!!
Block container reference set to nil 
BYE!!!
Block Container Ey I have been dealloc!

有人能告诉我我做错了什么吗?

尝试在将其设置为nil后添加[[NSGarbageCollector defaultCollector] collectIfNeeded];以强制进行垃圾回收。可能会起作用,也可能不会。 - Rory McKinnel
可能相关... https://dev59.com/MoDba4cB1Zd3GeqPD2z- - CrimsonChris
1个回答

3
您的dispatch_async代码块创建了一个强引用。当该代码块访问MyBlockContainer来获取它的myBlock属性时,会为这个代码块的生命周期创建一个对它的强引用。
如果您把代码改为下面这样:
 __weak void (^block)() = self.blockContainer.myBlock;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    block();
});

您应该看到您期望的结果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接