内存管理之 Block(weak-strong dance)

作者: 小橘爷 | 来源:发表于2016-03-14 14:54 被阅读1694次

如果觉得我写的还不错,请关注我的新浪微博@小橘爷,最新文章即时推送~

谨以此文献给酷爱 Block 的战友们——小橘爷

前言

Block 因为性能好,使用方便而为大多数 iOS 开发者所喜爱,但是 Block 的使用并非大家所想的那么简单。接下来就让我们从内存管理的角度看看,如何在 ARC 的环境下使用好 Block。

防止循环引用

众所周知,Block 在使用的时候默认会使对象的引用计数加一,所以我们需要使用 __weak 关键字来防止对象(主要是指拥有此 Block 所在对象的控制器)和 Block 循环引用。如下代码所示:

MyViewController *myController = [[MyViewController alloc] init…];
 // ...
MyViewController * __weak weakMyViewController = myController;
myController.completionHandler = ^(NSInteger result) {
    [weakMyViewController dismissViewControllerAnimated:YES completion:nil];
};

但是绝大多数人对于 Block 的使用就到此为止了……如果我要在 Block 里 removeObserver 你猜会发生什么?

崩溃!因为 weakMyViewController 被弱引用,在 ARC 的环境下(尤其还有可能伴随着多线程)随时可能被释放,这样就会导致因为解除 KVO 而引起 Crash。

虽然是小概率的事件,但是对于一个严格要求自己的程序员,再小概率的 Crash 触发也是不能放过的!

这时候你可能会想,我加一个if判断,看一下 weakMyViewController 是否为 nil 不就行了,比如这样:

MyViewController *myController = [[MyViewController alloc] init…];
// ...
MyViewController * __weak weakMyViewController = myController;
myController.completionHandler = ^(NSInteger result) {
    if (weakMyViewController != nil) {
        //在这里removeObserver
    }  
    [weakMyViewController dismissViewControllerAnimated:YES completion:nil];
};

我可以很负责任的告诉你,这条路是走不通的,因为 weakMyViewController 随时有可能被系统 release…… 那么我们究竟应该怎么办呢?

weak-strong dance

这时候我们进入到 AFNetworking 这个框架里,看看大牛是如何解决这个问题的~

__weak __typeof(self)weakSelf = self;
    AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status) {
    __strong __typeof(weakSelf)strongSelf = weakSelf;

    strongSelf.networkReachabilityStatus = status;
    if (strongSelf.networkReachabilityStatusBlock) {
        strongSelf.networkReachabilityStatusBlock(status);
    }

};

__strong __typeof(weakSelf)strongSelf = weakSelf;就是解决这个问题的关键~先将强引用的对象转为弱引用指针,防止了 Block 和对象之间的循环引用。再在 Block 的第一句代码出将 weakSelf 的弱引用转换成 strongSelf 这样的强引用指针,防止了多线程和 ARC 环境下弱引用随时被释放的问题(因为强引用的引用计数至少为1)。

这里大家一定要有一个认识,weakSelf 位于 Block 的外面,strongSelf 位于 Block 的里面。从内存管理的角度来看,weakSelf 是要比 strongSelf 的声明周期要长的。这样就形成了从弱引用到强引用,再从强引用到弱引用的一种变化,也称作 weak-strong dance。

那么到此就结束了吗?

苹果官方文档的解释(Transitioning to ARC Release Notes)

为了以防万一,我们还是来查阅一下苹果官方文档吧,毕竟这才是最权威的。

在里面我们看到了这样一串代码:

MyViewController *myController = [[MyViewController alloc] init…];
// ...
MyViewController * __weak weakMyController = myController;
myController.completionHandler = ^(NSInteger result) {
    MyViewController *strongMyController = weakMyController;
        if (strongMyController) {
            // ...
            [strongMyController dismissViewControllerAnimated:YES completion:nil];
            // ...
        }
        else {
            // Probably nothing...
        }
};

**if (strongMyController) **是这段代码的亮点。之所以在Block的代码执行之前加上这样一个判断,就是为了防止在把 weakSelf 转换成 strongSelf 之前 weakSelf 就已经为 nil 了,这样才能确保万无一失。

所以说,信自己不如信大牛,信大牛不如信苹果有空,就多翻翻官方文档吧

相关文章

网友评论

  • 为之则易ing:不是必须用__strong,根据需求用或者不用
    小橘爷:@iOS开发Go 这倒是,不过写上可以保证万无一失,也算是对代码的严谨性做一个交代吧。
  • Double丶K:春天都阻挡不了你写简书的脚步
    小橘爷:@凯文丶tableView 因为春天是那个的季节~😜
  • zhiyi:if (weakMyViewController != nil) { 的坑在哪里
    zhiyi:@杨浩宇 。。。好吧,感谢
    小橘爷:@zhiyi 即使你这么判断进入到里面后还是随时可能被释放,然后就crash⋯⋯
  • 杰米:真张见识了,以前一直是判断weakself是不是为nil!!😂
    小橘爷:@杰米 我们的项目里他们也是这么干的,直到后来偶发了block里removeObserver出现崩溃的情况,我才研究了一下,以后注意就好啦~
  • 依赖糊涂:都开始写文章了都~
    小橘爷:@请叫我赵先生 总结一下吗~
  • 4d4a34d7707f:大神好厉害,学习啦
    小橘爷:@处女座的拖延症晚期 过奖了,多多关注哦~
  • 191f22bf6d7f:用RAC也可以吧
    小橘爷:@开发 可以~
  • 上善若水zyz601:阔以
    小橘爷:@上善若水zyz601 😄
  • KavinZhou:长知识了,多谢楼主
    小橘爷:@ZK_520IT 不客气~每周更新,多多关注哦~

本文标题:内存管理之 Block(weak-strong dance)

本文链接:https://www.haomeiwen.com/subject/fpyflttx.html