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;
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With