This method should not be called on the main thread as it may lead to UI unresponsiveness

This the error that I have been getting.

“This method should not be called on the main thread as it may lead to UI unresponsiveness.”

The method called in the main thread –

func setupRevenueCat() {
Purchases.logLevel = .debug
Purchases.configure(withAPIKey: Constants.publicKey)
}

This was how I was calling the function

init(){
setupRevenueCat()
GADMobileAds.sharedInstance().start(completionHandler: nil)
FirebaseApp.configure()

        }

I wonder if any one can help me. Perhaps, I need to call the method in App Delegate. I was reading documentation from Revenue cat.

Louis Lee

Can you provide more code that illustrates the context in which you are performing the init()? Is it in a class or a struct?

When you post your code, after you have pasted it into your reply you can format the code nicely, by placing 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code. Like this:

```
Code goes here
```

The 3 back-ticks must be the ONLY characters on the line. The back-tick character is located on the same keyboard key as the tilde character ~ (which is located below the Esc key - QWERTY keyboard).

Alternatively after you paste in your code, select that code block and click the </> button on the toolbar. This does the same thing as manually typing the 3 back ticks on the line above and below your code.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing.

import SwiftUI
import Firebase
import GoogleMobileAds
import AppTrackingTransparency
import AdSupport
import RevenueCat


class AdsManager: NSObject, ObservableObject {

    final class interstitial:NSObject, GADFullScreenContentDelegate,ObservableObject{
        private var interstitial: GADInterstitialAd?
        override init() {
            super.init()
            requestInterstitialAds()
        }
        func requestInterstitialAds() {
            let request = GADRequest()
            request.scene = UIApplication.shared.connectedScenes.first as! UIWindowScene?
            ATTrackingManager.requestTrackingAuthorization (completionHandler: { status in
                GADInterstitialAd.load(withAdUnitID: Constants.adUnitInterstitialInstalled, request: request, completionHandler: { [self] ad, error in
                    if let error = error {
                        print("Failed to load ad with error: \(error)")
                        return
                    }
                    self.interstitial = ad
                    self.interstitial?.fullScreenContentDelegate = self
                })
            })
        }
        func showAd() {

            let root = UIApplication.shared.windows.first?.rootViewController
            if let fullScreenAds = interstitial {
                fullScreenAds.present(fromRootViewController: root!)
            } else {
                print("not ready")
            }
        }

    }
    }


class AdsViewModel: ObservableObject {
    static let shared = AdsViewModel()
    @Published var interstitial = AdsManager.interstitial()
    @Published var showInterstitial = false {
        didSet {
            if showInterstitial {
                interstitial.showAd()
                showInterstitial = false
  
            }
            else {
                    self.interstitial.requestInterstitialAds()
            }
        }
    }
}

@main
struct FetalGrowthPercentile: App {
    
    let adsVM = AdsViewModel.shared
    init(){
        setupRevenueCat()
        GADMobileAds.sharedInstance().start(completionHandler: nil)
        FirebaseApp.configure()
        
            }
 
    var body: some Scene {
        
        WindowGroup  {
        
            SwitchBoard()
                .environmentObject(adsVM)
                .environmentObject(fetusInfoModel())
                .onAppear(perform: UIApplication.shared.addTapGestureRecognizer)
   
        }
    }
    func setupRevenueCat() {
            Purchases.logLevel = .debug
            Purchases.configure(
                with: Configuration.Builder(withAPIKey: Constants.publicKey)
                    .with(appUserID: Constants.revenueCatAppUserId)
                      .build()
             )
    }
}

Previously, the code was working with no problem for one year. With the new xcode update, the problem began. I had blank out two lines and the problem disappeared –
setupRevenueCat()
FirebaseApp.configure()

I read RevenueCat Documentation. I tried using AppDelegate and have a function using didfinishrunningwithoptions. I would run into problem with unstable UI. I try updating cocoa pods but problem was still present.

Louis

I was trying to set up a project using your code but it quickly became apparent that there are too many Constants to resolve.

Maybe someone else can help.