таймер автоподключения на детской площадке
import PlaygroundSupport
import Combine
import Foundation
PlaygroundPage.current.needsIndefiniteExecution = true
var cancellable: AnyCancellable?
// start automatically
cancellable = Timer.publish(every: 1, on: .main, in: .default)
.autoconnect()
.sink {
print($0)
}
отпечатки
...
2020-02-24 02:22:20 +0000
2020-02-24 02:22:21 +0000
2020-02-24 02:22:22 +0000
2020-02-24 02:22:23 +0000
2020-02-24 02:22:24 +0000
2020-02-24 02:22:25 +0000
2020-02-24 02:22:26 +0000
...
ручной запуск / останов таймера
let timerPublisher = Timer.publish(every: 1.0, on: RunLoop.main, in: .default)
cancellable = timerPublisher
.sink {
print($0)
}
/// ...
// start publishing time
let cancellableTimerPublisher = timerPublisher.connect()
// stop publishing time
cancellableTimerPublisher.cancel()