I use a Timer in View to show time. In the View's onAppear() and onDisappear() method, the Timer works well.
But when I close the window, it seems that the onDisappear() method not be called, and the Timer never stops.
There is my test code:
import SwiftUI
struct TimerTest: View {
@State var date = Date()
@State var showSubView = false
@State var timer: Timer?
var body: some View {
ZStack{
if showSubView {
VStack {
Text(" Timer Stoped?")
Button("Back") {
self.showSubView = false
}
}
}
else {
VStack {
Button("Switch to subview"){
self.showSubView = true
}
Text("date: \(date)")
.onAppear(perform: {
self.timer = Timer.scheduledTimer(withTimeInterval: 1,
repeats: true,
block: {_ in
self.date = Date()
NSLog("onAppear timer triggered")
})
})
.onDisappear(perform: {
self.timer?.invalidate()
self.timer = nil
NSLog(" onDisappear stop timer")
// But if I close window, this method never be called
})
}
}
}
.frame(width: 500, height: 300)
}
}
So, how should I stop the timer correctly after the window closed?
And how could the View been notified when the window will be closed, aim to release some resources in the View instance.
( I have figured out a trick method using TimerPublisher replace Timer which would auto-stop after the window closed. But it doesn't resolve my confusion. )