Created
August 3, 2020 17:22
-
-
Save robertmryan/213c47fc379b6790615fc16390700736 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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