iOS dispatch_ After delayed execution results in delayed dealloc

Time:2022-5-5

Let’s build a new oneNewViewController, at the beginningViewControllerWrite the following code

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 50);
    btn.backgroundColor = [UIColor brownColor];
    [btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

-(void)jump{
    NewViewController *newVC = [[NewViewController alloc]init];
    [self presentViewController:newVC animated:YES completion:nil];
}

Then inNewViewControllerLi:

-(void)dealloc{
    NSLog(@"--------------dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 300, 100, 50);
    btn.backgroundColor = [UIColor brownColor];
    [btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self log];
        NSLog(@"--------------after");
    });
    
}

-(void)log{
    NSLog(@"--------------log");
}

-(void)jump{
    NSLog(@"--------------dismiss");
    [self dismissViewControllerAnimated:YES completion:nil];
}

After running, we jump into newvc and return. At this time:

iOS dispatch_ After delayed execution results in delayed dealloc

image.png

The results show that when we disassiss, newvc has not been released,deallocMethod indispatch_afterDelay method execution before leaving becausedispatch_afterStrong referenceSelf (i.e. newvc), and so on,selfTo be released. Let’s godealloc

Next, we aredispatch_afterInner handleFor self__ weakDecorate, blockselfChange toweakself, let’s follow the same operation process and see the results:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 300, 100, 50);
    btn.backgroundColor = [UIColor brownColor];
    [btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf log];
        NSLog(@"--------------after");
    });
    
}

iOS dispatch_ After delayed execution results in delayed dealloc

image.png

When we use weak to modify self,dispatch_afterNo strong referencesself, so wedissmissWhen,deallocI’ll leave right away, and then 10 seconds later,dispatch_afterThe execution function of will still execute. After is output, but log is not output because it is usedweakSelfdissmissAfter that, newvc has been released. At this time, the code[weakSelf log];Equivalent to[nil log];, so log will not be output.

Use attention

althoughdispatch_afterCall directly inselfIt won’t cause circular references, but when wedispatch_afterWhen the delay time is too long, you need to consider whether to release the current object in time. If necessary, try to use itweakSelfIn this way, if you really need to use self to complete some operations and release them, you can write code as needed.

Recommended Today

Modify user information changeinfo

When judging the persistence layer: Problem: there is such a problem when modifying user information. For example: the user’s email is not required. It was not empty originally. At this time, the user deletes the mailbox information and submits it. At this time, if it is not empty to judge whether it needs to be […]