why CATransaction.setCompletionBlock seems not being called

984 Views Asked by At

The following code works on all the devices simulators I have access. However, some users reported the issue that leads me to think the completion block is not being called in some situation. I am out of idea at this moment. Any suggestion?

CATransaction.begin()
CATransaction.setCompletionBlock {
  self.performSegue(withIdentifier: "ContractViewController", sender: sender.companyJob)
}

self.navigationController?.popViewController(animated: true)
CATransaction.commit()

BTW, what I wanted to achieve is to have the pop and push screen transition animations. At this point, I am open for any solution or workaround. Thanks in advance.

Extra document from doc:

/* Accessors for the "completionBlock" per-thread transaction property.
 * Once set to a non-nil value the block is guaranteed to be called (on
 * the main thread) as soon as all animations subsequently added by
 * this transaction group have completed (or been removed). If no
 * animations are added before the current transaction group is
 * committed (or the completion block is set to a different value), the
 * block will be invoked immediately. Added in Mac OS X 10.6. */
1

There are 1 best solutions below

3
AIex H. On

If you are using interface builder, you can use Unwind Segue and create a custom UIStoryboardSegue with completion block like this

class ViewController: UIViewController {
    @IBAction func unwind(_ segue: UIStoryboardSegue) {
        if let segue = segue as? StoryboardSegue {
            segue.completion = {
                // present or push in here
            }
        }
    }
}

class StoryboardSegue: UIStoryboardSegue {
    var completion: (() -> Void)?

    override func perform() {
        super.perform()
        self.destination.transitionCoordinator?.animate(alongsideTransition: { _ in

        }, completion: { _ in
             // do what you want after the animation is finished
           self.completion?()
        })
    }
}