As you might know, UIKit can't display alerts on top of each other. I'm having trouble writing SwiftUI application in WatchKit with a button, alert and action sheet.
### Sequence of events
1. Human taps the button
2. Action sheet appears with action confirmation
3. Human taps OK in the action sheet
4. Action block called which can return either success or error
5. In case of error, app needs to present an alert with its description
#### Expected
Alert with error description presented on top of a button
#### Current
> Warning: Attempt to present <PUICAlertSheetController: 0x823cbe00> on <_TtGC7SwiftUI19UIHostingControllerVS_7AnyView_: 0x80364150> whose view is not in the window hierarchy!
struct Favorits: View {
@State private var isConfirmSheetPresent = false
@State private var isAlertPresent = false
@State private var error: String?
var body: some View {
List {
Section(header: Text("Header")) {
ForEach(self.dataSource) { drink in
DrinkCell(drink: drink)
.onTapGesture {
self.selectedDrink = drink
self.isConfirmSheetPresent = true
}
}
}
}
.alert(isPresented: $isAlertPresent) {
Alert(title: Text(error!), message: nil, dismissButton: nil)
}
.actionSheet(isPresented: $isConfirmSheetPresent) {
ActionSheet(title: Text(""),
message: nil,
buttons: [.default(Text(""), action: { // Run action block })])
}
}
}
Action block
Model.sharedInstance.run() { (success, error) in
if success {
// Update UI
} else {
// There's no way to know when confirmation alert is present
// This won't work without a delay
self.error = error!.localizedDescription
self.isAlertPresent = true
}
}
### Solutions
I can delay alert and it fixes the issue, but I don't believe this is the right approach. Ideally I need to know exactly when action sheet is dismissed.
I also have tried this, but isConfirmSheetPresent set to false while dismiss animation is still running.
while !isConfirmSheetPresent {
// Display alert
}