Aim
I would like to display an alert, when the user dismisses the alert the next alert should be displayed.
If the user doesn't dismiss the first alert, the 2nd alert should wait till the user dismisses the first alert
Functionality like back pressure.
My Attempt:
Given below is code where I have attempted to do it based on an @ObservedObject and @Published property.
Problem:
Alerts are displayed but they don't wait for the alert to be dismissed.
Code:
ContentView:
Code Block swift import SwiftUI struct ContentView: View { @ObservedObject var model = Model() var body: some View { Text("Hello World!") .alert(item: $model.employee, content: makeAlert(forEmployee:)) } private func makeAlert(forEmployee employee: Employee) -> Alert { let dismissButton = Alert.Button.cancel((Text("Ok"))) return Alert(title: Text("New Employee"), message: Text(employee.name), dismissButton: dismissButton) } }
Model:
Code Block swift import Foundation class Model : ObservableObject { @Published var employee : Employee? private var index = 0 private lazy var timer = { Timer.scheduledTimer(withTimeInterval: 3, repeats: true) { [weak self] timer in self?.createNewEmployee() print("fired for \(self?.employee?.name ?? "none")") } }() init() { timer.fire() } func createNewEmployee() { guard let unicodeScalar = UnicodeScalar(index + 65) else { return } let name = String(Character(unicodeScalar)) employee = Employee(id: index, name: name) index += 1 } }
Employee:
Code Block swift import Foundation class Employee : Identifiable { var id : Int var name : String init(id: Int, name: String) { self.id = id self.name = name } }