我有一个@ObservedObject
在我看来:
struct HomeView: View {
@ObservedObject var station = Station()
var body: some View {
Text(self.station.status)
}
它根据a更新文本String
from Station.status
:
class Station: ObservableObject {
@Published var status: String = UserDefaults.standard.string(forKey: "status") ?? "OFFLINE" {
didSet {
UserDefaults.standard.set(status, forKey: "status")
}
}
但是,我需要更改的值status
in my AppDelegate
,因为那是我收到我的Firebase 云消息 https://firebase.google.com/docs/cloud-messaging/ios/receive:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// Print full message.
let rawType = userInfo["type"]
// CHANGE VALUE OF status HERE
}
但如果我改变status
UserDefaults
值在AppDelegate
- 在我看来它不会更新。
我的怎么可以@ObservedObject
在我看来,当status
变化?
编辑:忘记提及上述示例中使用了 SwiftUI 2.0 beta 版本。