Wednesday, September 28, 2022
HomeiOS Developmentios - Async/await equal of saving a completion block of a technique

ios – Async/await equal of saving a completion block of a technique


I’ve a perform in a NetworkManager class which hundreds an object Bar and returns it in a completion block. If my app is within the background, I save the completion block in a category 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 obtained, I name the perform once more and cross within the completion handler, such that the unique request can full and return again to the caller:

func applicationDidBecomeActive() {
  guard let request else {
    return
  }
  community.getBar(
    withRequestId: request.id,
    completion: request.completion
  )
  self.request = nil
}

This works properly. Nonetheless, I am now changing this perform to async however I am unable to determine the way to do the equal of this with out a completion block to avoid wasting.

func getBar(withRequestId requestId: String,) async throws -> Config {
  let applicationState = UIApplication.shared.applicationState
  if applicationState == .background {
    let configRequest = ConfigRequest(
      id: requestId,
      completion: completion // Cannot do that now
    )
    configManager.configRequest = configRequest
    return
  }
  ...
}

How can I do that, or one thing that is equal to this?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments