为什么alert dismiss在再次触发时不起作用?

Why alert dismiss does not work when it is triggered again?

我在向服务器发出请求之前显示加载器。收到应答后-调用停止加载程序的函数。

第一次很好用。但是当您重新进入该页面时,加载程序不会关闭并且它会永远旋转。 (我相信这是因为请求已经发生得更快,并且没有明显的延迟)。

为清楚起见,我在代码中添加了标记。
请告诉我问题是什么以及如何解决?

var alert : UIAlertController?

func showLoader(){
    self.alert = UIAlertController(title: nil, message: NSLocalizedString("pleaseWait", comment: "Текст ожидания загрузки"), preferredStyle: .alert)

    let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
    loadingIndicator.hidesWhenStopped = true
    loadingIndicator.style = UIActivityIndicatorView.Style.gray
    loadingIndicator.startAnimating();

    self.alert!.view.addSubview(loadingIndicator)
    present(self.alert!, animated: true, completion: nil)
    Logger.Log("Alert is presented")
}

func stopLoader(){
    if let _ = self.alert {
        Logger.Log("Loader isBeingPresented = \(self.alert!.isBeingPresented)")
        Logger.Log("Loader isBeingDismissed1 = \(self.alert!.isBeingDismissed)")

        self.alert!.dismiss(animated: false, completion: nil)

        Logger.Log("Loader isBeingDismissed2 = \(self.alert!.isBeingDismissed)")

        self.alert = nil

        Logger.Log("Loader dismissed")
    } else {
        Logger.Log("Alert failed")
    }
}

** Selected: 2019-03-01 17:00:00 +0000 ** //first try
** Alert is presented **
** File exist! **
** requestDone **
** Loader isBeingPresented = false **
** Loader isBeingDismissed1 = false **
** Loader isBeingDismissed2 = true **
** Loader dismissed **
** Selected: 2019-03-01 17:00:00 +0000 ** //second try
** Alert is presented **
** File exist! **
** requestDone **
** Loader isBeingPresented = false **
** Loader isBeingDismissed1 = false **
** Loader isBeingDismissed2 = false **
** Loader dismissed **

以下是您应该如何实现完成块:

1) viewDidLoad 调用 controller.start() 2) 演示者调用 view.showLoader() 完成回调 3) 在回调中你开始 NetworkController.getfiles() 4) 当 NetworkController.getfiles() 完成后,controller.callback 调用 view.stopLoader()