我认为从这个虚拟示例中非常清楚:如果您删除ForEach
代码行,神奇地,传播将流动并且时钟将滴答作响,否则一旦出现详细视图,它将冻结。
class ModelView: ObservableObject {
@Published var clock = Date()
init() {
Timer.publish(every: 1, on: .main, in: .default)
.autoconnect()
.print()
.assign(to: &$clock)
}
}
struct ContentView: View {
@StateObject var viewModel = ModelView()
var body: some View {
NavigationView {
List {
NavigationLink("Clock", destination: Text(viewModel.clock, formatter: formatter))
ForEach(0..<1) { _ in } // <- Remove this row and the clock will work
}
}
}
var formatter: DateFormatter {
let formatter = DateFormatter()
formatter.timeStyle = .medium
return formatter
}
}