LoginSignup
5
5

More than 5 years have passed since last update.

【Swift】Admobの取得をviewDidLoad()に置いてると「this application is modifying the auto layout engine...」と出る件の対処法

Posted at

背景

以前Admobを導入してみた記事を書きました。
http://seiya-orz.hatenablog.com/entry/2015/12/04/220806:

テスト運用していると、たまにWebThreadからExceptionを受け取って、アプリが止まることがありました。そのエラーの内容は「This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.」リリースした時にも起こるかもよ、とか言われてます。

このエラーは、要するに、バックグラウンドスレッドからレイアウトいじっちゃダメですよ、という話。

ただ、自分で書いている部分にレイアウトをいじっている部分は見当たらなく、なんとなくAdmobのせいなのではないか、と思い始めました。とくに、bannerViewに広告を挿しこんでいるのはバックグラウンドな感じがしましたし。

ということで調べてみると、たぶんこの記事が該当します。
https://groups.google.com/forum/?hl=ja#!category-topic/google-admob-ads-sdk/ios/_Pk6b8uQ7cg

解決方法

dispatch_async(dispatch_get_main_queue(), {
   //広告部分
   self.bannerView.adSize = kGADAdSizeSmartBannerPortrait
   self.bannerView.adUnitID = self.AdMobID  // テスト用ID
   self.bannerView.rootViewController = self
   let admobRequest:GADRequest = GADRequest()
   if self.AdMobTest {
      if self.SimulatorTest {
          admobRequest.testDevices = [kGADSimulatorID]
      } else {
          admobRequest.testDevices = [self.TEST_DEVICE_ID]
      }
   }
   self.bannerView.loadRequest(admobRequest)
})

このdispatch_async()内に広告取得にあたるコードを書き込むことで解決できるようです。今のところExceptionは発生していません。

Admobの公式に書いといてほしかった。もしかしたら書いてるのか?

おわり。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5