Nested blocks and references to self

岁酱吖の 提交于 2019-12-03 14:25:28

This is most likely occurring as you are targeting down to iOS 4. You should change it to be

__unsafe_unretained MyClass *weakWeakSelf = weakSelf;

With ARC

__weak __typeof__(self) wself = self;

Wihtout ARC

__unsafe_unretained __typeof__(self) wself = self;
tt.Kilew

With libextobjc it will be readable and easy:

- (void)doStuff
{
    @weakify(self); 
    // __weak __typeof__(self) self_weak_ = self;

    [self doSomeAsyncStuff:^{

        @strongify(self);
        // __strong __typeof__(self) self = self_weak_;

        // now you don't run the risk of self being deallocated
        // whilst doing stuff inside this block 
        // But there's a chance that self was already deallocated, so
        // you could want to check if self == nil

        [self doSomeAwesomeStuff];

        [self doSomeOtherAsyncStuff:^{

            @strongify(self);
            // __strong __typeof__(self) self = self_weak_;

            // now you don't run the risk of self being deallocated
            // whilst doing stuff inside this block 
            // Again, there's a chance that self was already deallocated, so
            // you could want to check if self == nil

            [self doSomeAwesomeStuff];

        }];
    }];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!