This is a similar problem to How to convert `VNDocumentCameraViewControllerDelegate` to Swift 6 Concurrency but one warning is new, so this may require different solution.
I have the following code:
@MainActor
final class InterstitialWrapper: NSObject {
...
}
extension InterstitialWrapper: GADFullScreenContentDelegate {
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
}
...
I got this error:
Main actor-isolated instance method ‘ad(_:didFailToPresentFullScreenContentWithError:)’ cannot be used to satisfy nonisolated protocol requirement; this is an error in the Swift 6 language mode
Then I use @preconcurrency
tag:
extension InterstitialWrapper: @preconcurrency GADFullScreenContentDelegate
The error becomes:
Non-sendable type ‘any GADFullScreenPresentingAd’ in parameter of the protocol requirement satisfied by main actor-isolated instance method ‘ad(_:didFailToPresentFullScreenContentWithError:)’ cannot cross actor boundary; this is an error in the Swift 6 language mode
Then I use @preconcurrency import
:
@preconcurrency import GoogleMobileAds
Then I got:
‘@preconcurrency’ attribute on module ‘GoogleMobileAds’ has no effect
I have verified that this is the only file that imports GoogleMobileAds
.
Note that this is similar to How to convert `VNDocumentCameraViewControllerDelegate` to Swift 6 Concurrency, but the warning about Non-sendable type 'any GADFullScreenPresentingAd'
is new. Also it’s not a system framework but a 3rd party framework (admob). So I am creating a new question here in case the solution is different.
2
Answers
Until the old api updates to use
MainActor
(I hope this is what you’re asking for) you can useassumeIsolated
. That makes your app crash (better than not noticing it) if you dont run the function in theMainActor
.Instead of marking a whole class as the
@MainActor
try marking separate methods as@MainActor
in your casehandleAdError
By doing so, it will not violate actor isolation rules. Also, make sure yourGADFullScreenPresentingAd
isSendable
If it is not feasible you might need to wrap the non-sendable type into@MainActor
and isolate the method. Finally, try using@preconcurrency
toGADFullScreenContentDelegate
method this will allow you to suppress warnings.After doing the above changes to you code it would look something similar to below: