Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Created August 3, 2020 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertmryan/213c47fc379b6790615fc16390700736 to your computer and use it in GitHub Desktop.
Save robertmryan/213c47fc379b6790615fc16390700736 to your computer and use it in GitHub Desktop.
import SwiftUI
import Combine
struct TimerTest: View {
@State var date = Date()
@State var showSubView = false
@State var timer: AnyCancellable? = nil
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 {
self.timer = Timer.publish(every: 1, on: .main, in: .common)
.autoconnect()
.sink { _ in
print("tick", Date())
}
}
.onDisappear {
self.timer?.cancel()
NSLog("🔶 onDisappear stop timer")
// But if I close window, this method never be called
}
}
}
}
.frame(width: 500, height: 300)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment