-[UIApplication delegate] must be called from main thread only

前端 未结 2 418
情话喂你
情话喂你 2020-11-28 11:02

This warning leads to a serious problem cause I really can\'t call the delegate outside of the main thread using Xcode 9 beta 2. Strange thing is that this was working when

相关标签:
2条回答
  • 2020-11-28 11:24

    Just call it from the main thread like this.

    Objective-C

    dispatch_async(dispatch_get_main_queue(), ^{
      [[UIApplication delegate] fooBar];
    });
    

    Swift

    DispatchQueue.main.async {
      YourUIControlMethod()
    }
    

    Reaching out to your app delegate like this, is a hint that your architecture could use a little cleanup.

    You can call delegates from any thread you want. You only need to make sure you're on the main thread for UIKit calls. Or that you're on the correct thread your CoreData objects expect. It all depends on the API contract your objects have.

    0 讨论(0)
  • 2020-11-28 11:45

    In Swift, you could also use DispatchQueue.main.async to call the UI controlling method from the main thread

    DispatchQueue.main.async {
        YourUIControlMethod()
    }
    
    0 讨论(0)
提交回复
热议问题