0

I am using Xcode 10.1 and am an inexperienced programmer in Swift 4.

I can execute the code below (animating a UICollectionView cell when selected) with no warnings or errors:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    guard let cell = collectionView.cellForItem(at: indexPath) as? CompanyCollectionViewCell else { return }

    UIView.animate(withDuration: 0.1, delay: 0, options: [.curveEaseOut], animations: {
        cell.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
    }) { (finished) in
        UIView.animate(withDuration: 0.08, delay: 0, options: [.curveEaseIn], animations: {
            cell.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
        }, completion: { (finished) in
            DispatchQueue.main.async {
                self.performSegue(withIdentifier: "showTradeView", sender: self)
            }
        })
    }
}

...but I am concerned about retain cycles within the animation closures.

Do I need to use [unowned self] in the last 'completion:' closure? Do I need to use [unowned cell] in the first animation closure?

and finally (slightly unrelated, sorry) Do I have to call the performSegue code inside DispatchQueue.main, as it seems quite happy to work without this?

Any help is much appreciated.

  • 1
    *"Do I have to call the performSegue code inside DispatchQueue.main"* - no, the completion block is called on the main queue. – rmaddy Dec 06 '18 at 18:17
  • *"but I am concerned about retain cycles"* - then test to see if you actually have a retain cycle. Use the debugger and see if you have any leaks or reference cycles. – rmaddy Dec 06 '18 at 18:19
  • Which 2 objects would have strong references to each other in this case? – meggar Dec 06 '18 at 18:22
  • @rmaddy is correct about the `performSegue(withIdentifier:sender:)`. And about the cycles you can check [this question](https://stackoverflow.com/questions/29770743/do-we-need-to-use-weak-self-inside-uianimationblocks-in-arc). – Ángel Téllez Dec 06 '18 at 18:59

0 Answers0