Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

So I have created the following code:

* 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

So I was expecting that createBlockWeakyfy will generate the scenario I wanted to replicate but I haven't manage to do it.

The output is the same for createBlockWeakyfy and 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?

like image 904
agy Avatar asked Jul 22 '15 15:07

agy


1 Answers

Your dispatch_async block is a creating a strong reference. When that block accesses your MyBlockContainer to get its myBlock property, it creates a strong reference to it for the life of that block.

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.

like image 85
Mean Dinosaur Avatar answered Oct 21 '22 14:10

Mean Dinosaur