美文网首页
iOS——记一次block的循环引用

iOS——记一次block的循环引用

作者: Jerry在种草 | 来源:发表于2016-04-13 08:24 被阅读161次

    最近在做的小项目出现了一个问题。当presentViewController时内存上升(在debug navigator下观察),dissmissViewController时竟然没有下降。我第一反应就是哪里出现循环引用了。于是我手工遍历一遍代码,还好代码量不是太多,很快就找到问题的根源。

    下文的block其实就是swift中的closure闭包,两者非常想想。

    问题描述:

    有时候为了减少重复代码,我们会将重复的代码抽取出去放在一个函数里面,然后根据不同参数来调用该函数。

        func editAccountBook(item:AccountBookBtn?, indexPath:NSIndexPath, sureBlock:(String, String)->Void){
            customAlertView.title = item?.btnTitle ?? ""
            customAlertView.initChooseImage = item?.backgrountImageName ?? "book_cover_0"
            customAlertView.cancelBlock = {[weak self] in
                if let strongSelf = self{
                    strongSelf.customAlertView.removeFromSuperview()
                }
            }
            customAlertView.sureBlock = sureBlock
            UIApplication.sharedApplication().keyWindow?.addSubview(self.customAlertView)
        }
    

    比如上面这个函数,目光集中在倒数第二行的sureBlock,这个sureBlock是外部传进来的。我们知道Block的原理就是一个结构体,像函数传参这种一般都是传sureBlock的结构体指针。在使用属性的时候,swift允许我们不加self,所以customAlerView是被self引用到的。而只要这个sureBlock里面有对self的强引用,将sureBlock赋值给customAlerView.sureBlock的话就会造成循环易用。所以说,抽代码要小心呐。

    再看看editAccountBook这个函数被调用的地方,一开始我是这么写的

        let block:(String,String)->Void = {(title, imageName) in
            //建一个数据库
            let currentTime = Int(NSDate().timeIntervalSince1970)
            let dbName = customAccountName + "\(currentTime)" + ".db"
            let item = AccountBookBtn(title: title, count: "0笔", image: imageName, flag: false, dbName: dbName)
            //插入账本
            self.mainVCModel.addBookItemByAppend(item)
            self.mainView.accountBookBtnView.insertItemsAtIndexPaths([indexPath])
            //退出alertview
            self.customAlertView.removeFromSuperview()
        }
        editAccountBook(nil, indexPath: indexPath, sureBlock: block)
    

    当时觉得这个block是临时生成的,里面虽然引用到self,应该也没什么关系。殊不知这个block在传给editAccountBook这个函数的时候就间接地被self引用到了,所以就造成了循环引用。所以用block真是要特别小心。特别是一些自己写的函数。

    解决办法

    解决办法很简单,在block的开头加一些list就好。如下

        editAccountBook(nil, indexPath: indexPath){[weak self](title, imageName) in
            if let strongSelf = self{
                //建一个数据库
                let currentTime = Int(NSDate().timeIntervalSince1970)
                let dbName = customAccountName + "\(currentTime)" + ".db"
                let item = AccountBookBtn(title: title, count: "0笔", image: imageName, flag: false, dbName: dbName)
                //插入账本
                strongSelf.mainVCModel.addBookItemByAppend(item)
                strongSelf.mainView.accountBookBtnView.insertItemsAtIndexPaths([indexPath])
                //退出alertview
                strongSelf.customAlertView.removeFromSuperview()
            }
        }
    

    这里用了swift的省略写法,当函数最后一个参数是block时,可以将整个block移到函数的末尾。可以看到这里使用了[weak self],这一句会将selfwrap成一个optional,所以在使用的时候得unwrap。其中的if let strongSelf = self就是unwrap。看到这里,用Objc开发的同学应该很熟悉了,在ARC下block的写法和swift里的思想都是一模一样的,格式有点不同罢了。

    自己写的总结,方便以后查看。

    相关文章

      网友评论

          本文标题:iOS——记一次block的循环引用

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