I have a function in a NetworkManager class which loads an object Bar and returns it in a completion block. If my app is in the background, I save the completion block in a class FooManager like this:
func getBar(
withRequestId requestId: String,
completion: (Bar) -> Void
) {
let applicationState = UIApplication.shared.applicationState
if applicationState == .background {
let request = BarRequest(
id: requestId,
completion: completion
)
fooManager.request = request
return
}
...
}
Then, in FooManager when the applicationDidBecomeActive notification is received, I call the function again and pass in the completion handler, such that the original request can complete and return back to the caller:
func applicationDidBecomeActive() {
guard let request else {
return
}
network.getBar(
withRequestId: request.id,
completion: request.completion
)
self.request = nil
}
This works well. However, I'm now converting this function to async but I can't figure out how to do the equivalent of this without a completion block to save.
func getBar(withRequestId requestId: String,) async throws -> Config {
let applicationState = UIApplication.shared.applicationState
if applicationState == .background {
let configRequest = ConfigRequest(
id: requestId,
completion: completion // Can't do this now
)
configManager.configRequest = configRequest
return
}
...
}
How can I do this, or something that's equivalent to this?