简体   繁体   中英

Swift closure stored as a variable produces memory leak

I have a retain cycle when defining a closure as a variable.

The variable is defined as below:

public class MyCell: UICollectionViewCell {
    public var callback: ((MyCell)->Void)?
}

If I use delegates instead of closures, the retain cycle disappears, but I would like to know how can it be defined with closures for future cases.

I tried to set callback variable as weak , but, as I suppose, weak attribute can only be applied to class and class-bound protocol types.

EDIT

Usage:

class CustomController: UIViewController {
   private func onActionOccurs(_ cell: MyCell) {
       cell.backgroundColor = .red // For example
   }

   // After dequeuing the cell:
   {
       cell.callback = onActionOccurs(_:)
   }
}

Thanks

If you don't need to use self, then you can use the cell itself, and modifying your closure implementation in cell like this

public class MyCell: UICollectionViewCell {
    public var callback: (()->Void)?
}

then you can use it, like this example

class CustomController: UIViewController {
   private func onActionOccurs(_ cell: MyCell) {
       cell.backgroundColor = .red // For example
   }

   // After dequeuing the cell:
   {
       cell.callback = {
         cell.backgroundColor = .red // For example
        }
   }
}

but if you need to use a ViewController method then you need to use the [weak self] capture list If you need use UIViewController methods

class CustomController: UIViewController {
   private func onActionOccurs(_ cell: MyCell) {
       cell.backgroundColor = .red // For example
   }

   // After dequeuing the cell:
   {
       cell.callback = { [weak self] in
         guard self != nil else {
            return
         }

         self!.viewControllerMethod()
        }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM