Ios 第一个函数仅在执行另一段代码后才开始执行

Ios 第一个函数仅在执行另一段代码后才开始执行,ios,swift,uiactivityindicatorview,Ios,Swift,Uiactivityindicatorview,我有显示和隐藏活动指示器的功能。我想在从tableview中删除行时显示活动指示器。但是当我调用delete action时,应该首先开始工作的函数showActivityIndicator(),只有在执行for,tableview.deleteRows和deleteAction中的另一个代码之后才开始工作。为什么showActivityIndicator()最终要开始工作?如何强制函数在执行另一段代码之前显示ActivityIndicator class TestClass: UIViewCo

我有显示和隐藏活动指示器的功能。我想在从tableview中删除行时显示活动指示器。但是当我调用delete action时,应该首先开始工作的函数
showActivityIndicator()
,只有在执行
for
tableview.deleteRows
deleteAction
中的另一个代码之后才开始工作。为什么
showActivityIndicator()
最终要开始工作?如何强制函数在执行另一段代码之前显示ActivityIndicator

class TestClass: UIViewController, UITableViewDataSource, UITableViewDelegate {


var container: UIView = UIView()
var loadingView: UIView = UIView()
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

// MARK: - showActivityIndicator
func showActivityIndicator() {

    // Container that equal iPhone screen size
    container.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
    container.center = CGPoint(x: container.frame.midX, y: container.frame.midY)
    container.backgroundColor = UIColor.black.withAlphaComponent(0.6)

    // View for activity indicator
    loadingView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
    loadingView.center = container.center
    loadingView.backgroundColor = UIColor.black
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10

    // Setup activity indicator
    activityIndicator.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
    activityIndicator.center = CGPoint(x: loadingView.frame.size.width / 2, y: (loadingView.frame.size.height / 2))

    loadingView.addSubview(activityIndicator)
    container.addSubview(loadingView)

    activityIndicator.startAnimating()

    // Show activity indicator
    let window = UIApplication.shared.keyWindow!
    window.addSubview(container)

}

// MARK: - hideActivityIndicator
func hideActivityIndicator() {
    activityIndicator.stopAnimating()
    container.removeFromSuperview()
}

// MARK: - Delete action(swipe)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    var swipe = UISwipeActionsConfiguration()

    switch segmentedControl.selectedSegmentIndex {

    case 0:

        let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, sourceView, completionHandler) in

            self.showActivityIndicator()

            for i in 0...10000 {

              print("i:",i)

            }

            self.tableView.deleteRows(at: [indexPath], with: .fade)

            completionHandler(true)

            self.fetchData()

            self.tableView.reloadData()

            // self.hideActivityIndicator()

        }

        let swipe1 = UISwipeActionsConfiguration(actions: [deleteAction])

        swipe = swipe1

    case 1:
        break
    default:
        break
    }

    return swipe

}
}