I have an UIViewController extension which I am using to display alerts in any view controller. It worked fine until this weird use case happened:
extension UIViewController {
func showModal(title: String, msg: String, handler: ((UIAlertAction) -> Void)? = nil) {
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
DispatchQueue.main.async {
self.present(alert, animated: true, completion: nil)
}
}
}
Inside a view controller I tap a button which triggers a network fetch:
@IBAction func didTapSave(_ sender: UIButton) {
Task {
let result = await BalanceManager.shared.fetchAllBalances()
switch result {
case .success(_):
self.showModal(title: "", msg: "account successfully saved") { (_) in
//This code always gets executed
self.navigationController?.popViewController(animated: true)
}
case .failure(let failure):
self.showModal(title: "", msg: "failure") { (_) in
//Code in here is not executed
print("Failure closure")
}
}
}
I don't understand why on case ".failure" the closure for showModal does not execute. If I set a breakpoint on the line with self.showModal the code gets there but does not execute the closure when I tap "OK" on the popup