Locking thread with DispatchGroup

In my AppDelegate I'm setting a custom NSObject subclass as my UNUserNotificationCenterDelegate. When the notification comes in, depending on type, I need to make a web call, which is of course asynchronous. However, I can't exit the delegate method until that web call returns, since I have to call the completionHandler passed into userNotificationCenter(_:willPresent:withCompletionHandler).


I was thinking I could just do the below, but the wait blocks the main thread, and the web call is never actually kicked off. How do I work around this?


func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
   // Stuff
   let group = DispatchGroup()
   group.enter()

   AlamofireAsyncWebCall.onComplete {
       [unowned self] response in
        defer { group.leave() }
       // stuff here
   }

   group.wait()

   completionHandler([])
}



I'm thinking unowned is safe there since this delegate is strongly held by the AppDelegate, and thus it couldn't go away before the web returned or the app would have actually exited.

Locking thread with DispatchGroup
 
 
Q