Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift async/await - Ignore "Consider using asynchronous alternative function" warning

I have some objective c networking code with completion handler for which swift has automatically generated an async version. I'm calling it at the end of an async function, but in this case I don't need to wait for it. I'm just passing nil for the completion handler. The compiler is giving me a warning:

Consider using asynchronous alternative function

Task {

  let foo = await bar() // Yes, using async/await here

  // Objc method. Completion is nil because I don't care about the result.
  noNeedToWaitForThis(completion: nil) // WARNING: Consider using asynchronous alternative function

}

Is there a swifty way to avoid this error? Or is the only option wrapping it in another function?

So far the best I've found is to just use a closure:

Task {
  ...
  {
    noNeedToWaitForThis(completion: nil) // No more warning
  }()
}

This is in Xcode 13.1.

[EDIT] Example objective c method with callback:

typedef void (^ResponseBlock)(BOOL success, id _Nullable object);
- (void)noNeedToWaitForThisWithCompletion:(nullable ResponseBlock)completion;
like image 548
arsenius Avatar asked Sep 14 '25 08:09

arsenius


1 Answers

Prior to Xcode 14.3, the easiest way to silence this warning was to use let _ = ....

Task {
  ...
  let _ = noNeedToWaitForThis(completion: nil) // Warning is silenced
}

Unfortunately, now the only way seems to be an anonymous closure:

Task {
   ...
   { noNeedToWaitForThis(completion: nil) }() // Warning is silenced
}
like image 57
arsenius Avatar answered Sep 17 '25 00:09

arsenius