al_mota
al_mota

Reputation: 471

Init a var in main thread

If I init a UIWindow like this:

open lazy var whistleWindow: UIWindow = UIWindow()

I get an runtime issue:

UIWindow() must be called from main tread only

enter image description here

Therefore, I tried to do something like this:

open lazy var whistleWindow: UIWindow = {

  var window:UIWindow!

  DispatchQueue.main.sync {
    window = UIWindow()
  }

  return window
}()

Sadly I get this error at runtime:

Thread 1: EXC_BREAKPOINT (code=1, subcode=0x10497bba4)

What could I do?

Upvotes: 2

Views: 1327

Answers (1)

al_mota
al_mota

Reputation: 471

open lazy var whistleWindow: UIWindow = {
    DispatchQueue.main.sync {
        return UIWindow()
    }
}()

Upvotes: 1

Related Questions