简体   繁体   English

Swift 3 - 在异步作业后关闭视图控制器

[英]Swift 3 - Dismissing view controller after async job

I am using Swift 3, Xcode 8.2. 我使用的是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. 按下按钮时,它会运行下面的buttonPressed功能。 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. 一旦作业完成,我希望活动指示器被关闭,视图控制器也要关闭它最初来自的VC。

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>)2017-03-03 21:37:56.833899 EOB-Reader [27710:6869686] [断言] transitionViewForCurrentTransition没有设置,演示控制器在演示期间被解雇了? (<_UIAlertControllerAlertPresentationController: 0x109e3b190>) (<_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. 现在动画通常需要大约0.25秒的时间才能完成其操作。

  • 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. 请记住,当您调度异步时,通常会调度后台线程以防止UI阻塞。 All on screen (UI) components need be modified on the main thread. 需要在主线程上修改所有屏幕(UI)组件。 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 this.dismissviewcontroller:动画

Will update with code shortly. 将很快更新代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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