简体   繁体   English

Swift 本地通知检查器仅主线程

[英]Swift Local Notifications Checker Main Thread Only

I have a method with a completion that returns a bool.我有一个完成返回布尔值的方法。 But I cant figure out how to get around this error.但我不知道如何解决这个错误。

Error错误

UISwitch.isOn must be used from main thread only

Button Action按钮动作

@IBAction func notificationSwitch(_ sender: Any) {
    
    LocalNotification().checkEnabled(completion: { (success) -> Void in
        
        // When download completes,control flow goes here.
        if success == false{
            print("Cant Turn On")
            self.notificationToggle.isOn = false
        } else {
            print("Can Turn On")
            if self.notificationToggle.isOn == true {
                self.notificationToggle.isOn = false
            } else {
                self.notificationToggle.isOn = true
            }
        }
    })
}

also already tried wrapping the LocalNotifications().... in DispatchQueue.main.async but still get the same error也已经尝试将LocalNotifications()....包装在DispatchQueue.main.async中,但仍然出现相同的错误

You're almost there.您快到了。 It is not the checkEnabled that needs to be wrapped in the call to get onto the main thread, but the stuff "inside" it:不是checkEnabled需要包装在调用中才能进入主线程,而是它“内部”的东西:

    LocalNotification().checkEnabled(completion: { (success) -> Void in
        DispatchQueue.main.async {
            if success == false {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM