title: Calling an asynchronous method (getNotificationSettings) in the onAppear() method becomes nil - SwiftUI MVVM
body: <p>I have a <code>NotificationsManager</code> class to manage all notification-related code, including the code to check if the user has turned off local notifications. I use this class in multiple parts of my code and it works fine, my issue is that I want to call the <code>getNotificationSettings</code> through the <code>NotificationsManager</code> class in the onAppear method in SwiftUI but it always becomes nil. I know it has to do something with the fact that the <code>getNotificationSettings</code> is an asynchronous method, but I'm not sure how to call it and get the result on the onAppear method.</p>
  <p>In the following code, <code>notificationManager.notificationAuthorizationStatus</code> always becomes <code>nil</code> in the <code>onAppear</code> method.</p>
  <p><strong>Any idea how can I call the <code>reloadAuthorizationStatus()</code> method in the <code>onAppear</code> method in <code>SwiftUI</code> and right after check the <code>notificationAuthorizationStatus</code> property?</strong></p>
  <h3>NotificationsManager Class</h3>
  <pre><code>class NotificationsManager: ObservableObject{
      @Published private(set) var notificationAuthorizationStatus: UNAuthorizationStatus?
  
      func reloadAuthorizationStatus() {
          UNUserNotificationCenter.current().getNotificationSettings { settings in
              DispatchQueue.main.async {
                  self.notificationAuthorizationStatus = settings.authorizationStatus
              }
          }
      }
  }
  </code></pre>
  <h3>ContentView</h3>
  <pre><code>struct ContentView: View {
      @StateObject private var notificationManager = NotificationsManager()
  
      var body: some View {
          NavigationView {
              Form {
                  // textFields...
              }
              .onAppear(){
                  notificationManager.reloadAuthorizationStatus() 
                  // here the notificationAuthorizationStatus is always nil              
                  switch notificationManager.notificationAuthorizationStatus{
                  case .denied:
                      hasAuthorized = false
                  default:
                      break
                  }
              }
          }
      }
  }
  </code></pre>
  <p>If I add a 1-second delay before calling the <code>notificationAuthorizationStatus</code> it works but I know this is not right since sometimes it may take longer than just a second to get the results.</p>
  <h3>Call with a 1 Second Delay</h3>
  <p>Works but I don't feel it's the right thing to do</p>
  <pre><code>.onAppear(){
      notificationManager.reloadAuthorizationStatus()                
      let secondsDelay = DispatchTime.now() + 1
      DispatchQueue.main.asyncAfter(deadline: secondsDelay){
          switch notificationManager.notificationAuthorizationStatus{
          case .denied:
              hasAuthorized = false
          default:
              break
          }
      }
  }
  </code></pre>