简体   繁体   中英

Swift 3 - Dismissing view controller after async job

I am using Swift 3, Xcode 8.2.

I have a view controller on which there is a button. When the button is pressed, it runs the buttonPressed function below. It kicks off an asynchronous job which takes a few seconds to complete during which a spinning gear activity indicator pops up. Once the job is finished, I want the activity indicator to dismiss and the view controller to dismiss as well to the VC that it originally came from.

func buttonPressed() {

    // Set up some stuff here
    ...

    // this code block here is to asynchronously run the processing job
    // while the activity indicator gear spins
    self.displaySpinningGear()
    DispatchQueue.main.async {
        self.doSomeJobProcessing()
    }
    self.dismiss(animated: true, completion: nil)

    // also dismiss the camera view
    self.presentingViewController?.dismiss(animated: true, completion: nil) // error here
}

However, I get an error on that last statement:

[Assert] Trying to dismiss the presentation controller while transitioning already. (<_UIAlertControllerAlertPresentationController: 0x109e3b190>) 2017-03-03 21:37:56.833899 EOB-Reader[27710:6869686] [Assert] transitionViewForCurrentTransition is not set, presentation controller was dismissed during the presentation? (<_UIAlertControllerAlertPresentationController: 0x109e3b190>)

Any help would be greatly appreciated.

Perform anyone transition of these at a time.

if let viewController = presentingViewController {
  // This block will dismiss both current and a view controller presenting current.
  viewController.dismiss(animated: true, completion: nil)
} else {
  // This block will dismiss only current view controller
  self.dismiss(animated: true, completion: nil)
}

Two transition operations cannot be performed simultaneously.

  • In your source code you are dismissing current view controller with animation. Now animation generally takes time around 0.25 second to complete its operation.

  • Now, in next line you are trying to dismiss a ( ** ) view controller that has presented current view controller. (In your case ( ** ) view controller is also presented by some other view controller.) so, within a fraction of seconds/milliseconds ( ** ) view controller will also try to dismiss it self with animation.

  • At this time your current view controller is being dismissed and (**) view controller will also start dismiss operation. So, both operations conflict each other on main executing thread. And results into an issue, you are facing.

Also, share your code for block

self.doSomeJobProcessing()

Share here, if you've set any other view/controller transition operations here.

Remember that when you dispatch asynchronous you're typically dispatching to background thread to prevent UI blocking. All on screen (UI) components need be modified on the main thread. So start your progress spinner from the main thread, dispatch your heavy lifting to the background, then redispatch to the main thread that you'd like to. this.dismissviewcontroller:animated

Will update with code shortly.

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