繁体   English   中英

在主线程上接收@Published

[英]Receiving @Published on main thread

我有以下代码给我以下错误消息:

  class Variables: ObservableObject {
    @Published var isNotificationsEnabled: Bool = false
  }

  @EnvironmentObject var v: Variables

  func notificationsEnabled() {
    let current = UNUserNotificationCenter.current()

    current.getNotificationSettings(completionHandler: { (settings) in
        if settings.authorizationStatus == .notDetermined {
          v.isNotificationsEnabled = false
        } else if settings.authorizationStatus == .denied {
            // Notification permission was previously denied, go to settings & privacy to re-enable
          v.isNotificationsEnabled = false
        } else if settings.authorizationStatus == .authorized {
            // Notification permission was already granted
          v.isNotificationsEnabled = true
        }
    })
  }
Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.

我能做些什么来解决这个问题? 我无法更改已发布的变量

if包装在DispatchQueue.main.async

暂无
暂无

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

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