skip to Main Content

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


  1. Until the old api updates to use MainActor (I hope this is what you’re asking for) you can use assumeIsolated. That makes your app crash (better than not noticing it) if you dont run the function in the MainActor.

    nonisolated func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
      MainActor.assumeIsolated {
        //Code Goes here
      }
    }
    
    Login or Signup to reply.
  2. Instead of marking a whole class as the @MainActor try marking separate methods as @MainActor in your case handleAdError By doing so, it will not violate actor isolation rules. Also, make sure your GADFullScreenPresentingAd is Sendable If it is not feasible you might need to wrap the non-sendable type into @MainActor and isolate the method. Finally, try using @preconcurrency to GADFullScreenContentDelegate method this will allow you to suppress warnings.

    After doing the above changes to you code it would look something similar to below:

    import GoogleMobileAds
    
    final class InterstitialWrapper: NSObject {
      // add your other properties here
    }
    
    extension InterstitialWrapper: @preconcurrency GADFullScreenContentDelegate {
      func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
        handleAdError(ad: ad, error: error)
      }
    
      @MainActor
      private func handleAdError(ad: GADFullScreenPresentingAd, error: Error) {
        // add the rest of the code behavior here
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search