Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Wait for existing transition or presentation to finish

I thought this was simple: Whenever I get a remote notification while the app is running present a UIAlertController with the message.

The problem is that presenting that alert might happen just as the main app is about to push/present another view controller. In this case, I get ugly error messages like

pushViewController:animated: called on <UINavigationController 0x7f400c00> while an existing transition or presentation is occurring; the navigation stack will not be updated.

and the app might get into inconsistent states this way. How can I arrange view controller transitions such that they do not get in conflict like this?

like image 376
Captain Fim Avatar asked May 04 '17 10:05

Captain Fim


1 Answers

Separating the controllers into two UIWindows solves this problem. Instead of just presenting the alert on one of the app's view controllers you create a new window like this:

let screen = UIScreen.main
let screenBounds = screen.bounds
let alertWindow = UIWindow(frame: screenBounds)
alertWindow.windowLevel = UIWindowLevelAlert
let vc = UIViewController()
alertWindow.rootViewController = vc
alertWindow.screen = screen
alertWindow.isHidden = false
vc.present(alert, animated: true)

Now the view controllers in the app's main window can push and present other controllers simultaneously to showing alerts.

like image 184
Captain Fim Avatar answered Sep 27 '22 22:09

Captain Fim