Swift 3 - 与在后台运行的长功能异步显示警报控制器

时间:2017-03-30 20:58:09

标签: ios swift swift3

我正在使用Swift 3。

我要做的行为是:用户点击按钮,旋转齿轮警报控制器显示,同时启动长时间运行的功能。一旦该功能完成执行,旋转齿轮就会消失,视图控制器就会解除。

以下代码启动doProcessing功能,但在视图解除之前大约一秒钟内不会显示旋转齿轮。所以这不是很正确。

func displaySpinningGear() {

    print("display spinning gear")
    // show the alert window box
    let activityAlertController = UIAlertController(title: "Processing", message: "Please wait while the photo is being processed.", preferredStyle: .alert)

    //create an activity indicator
    let indicator = UIActivityIndicatorView(frame: activityAlertController.view.bounds)
    indicator.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    indicator.hidesWhenStopped = true
    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray

    //add the activity indicator as a subview of the alert controller's view
    activityAlertController.view.addSubview(indicator)
    indicator.isUserInteractionEnabled = false // required otherwise if there buttons in the UIAlertController you will not be able to press them
    indicator.startAnimating()

    print("start animating")

    self.present(activityAlertController, animated: true, completion: nil)
}

func onButtonClick() {
    self.displaySpinningGear()
    DispatchQueue.main.async {
        self.doProcessing() // long running function
    }

    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)
    }
}

下面的代码启动doProcessing函数,但视图立即解除,我可以从控制台告诉我doProcessing函数仍在运行。这也不对。

function onButtonClick() {
    DispatchQueue.global(qos: .background).async {
        print("Processing")
        self.doProcessing() // run in background

        DispatchQueue.main.async {
            self.displaySpinningGear()
        }
    }

    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)
    }
}

如何在显示旋转装置时启动后台功能,并在后台功能运行(而不是之前)时关闭视图和警报控制器?

修改

根据@ Honey在评论中的建议尝试移动代码以在背景块之外旋转齿轮,但无济于事。当进程函数仍在处理时,视图立即解除(我可以通过print语句告诉)。

func onButtonClick() {
    DispatchQueue.main.async {
        self.displaySpinningGear()
    }
    DispatchQueue.global(qos: .background).async {
        print("Processing")
        self.doProcessing() // run in background
    }

    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)
    }
}

2 个答案:

答案 0 :(得分:0)

从长时间运行的函数进行回调,因此当它结束时返回一个值并捕获它以消除警报。

试一试:

EXEC sys.sp_refreshsqlmodule 'TVF_xyz'

希望它可以帮到你

答案 1 :(得分:0)

我遇到了同样的问题并尝试了许多不同的东西,这是有效的:

activityView.alpha = 0.8
DispatchQueue.global(qos: .default).async(execute: {
    DispatchQueue.main.async(execute: {
        self.performSegue(withIdentifier: "cropToProcessed", sender: self)
    })
})

基本上我最初将活动指示器的alpha设置为0.0,当按下按钮时我将其设置为0.8并在viewWillDisappear中将其设置回0.0并且它可以正常工作