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

前言

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];
};

 

 

 

我可以很负责任的告诉你,这条路是走不通的……那么我们究竟应该怎么办呢?

 

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 了,这样才能确保万无一失。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值