我有以下代码:
我可以调试并查看通知是否被发送。该行“数据已导入”正在打印。我想更新self.isDataImported属性,然后刷新我的视图。
@State private var isDataImported: Bool = false
init() {
NotificationCenter.default.addObserver(forName: .onDataImported, object: nil, queue: nil) { [self] notification in
print("Data has been imported...")
DispatchQueue.main.async {
self.isDataImported = true
print(self.isDataImported) // prints out false
}
}
}
我可以调试并查看通知是否被发送。该行“数据已导入”正在打印。我想更新self.isDataImported属性,然后刷新我的视图。
if isDataImported {
ShowDataView()
} else {
ProgressView()
}
但是ShowDataView视图从未显示,因为isDataImported始终为false。我错过了什么?