與 Timer 密切使用的 Operator.
var set = Set<AnyCancellable>()
func SOMESETUP() {
UITextField()
.publisher(for: \.text)
.debounce(for: .microseconds(500),
scheduler: RunLoop.main)
.sink{print($0)}
.store(in: &set)
}
lastest
決定是否發佈定時前後的元素.var set = Set<AnyCancellable>()
Timer.publish(every: 1, on: .main, in: .common)
.autoconnect()
.throttle(for: .seconds(2),
scheduler: RunLoop.main,
latest: false)
.sink{print($0)}
.store(in: &set)
var set = Set<AnyCancellable>()
let start = Date()
print(start)
Timer.publish(every: 1, on: .main, in: .common)
.autoconnect()
.delay(for: .seconds(5),
scheduler: RunLoop.main)
.first()
.sink(receiveCompletion: {completion in print(completion, Date())}){print($0)}
.store(in: &set)
/* console:
00:00
00:01
finished 00:06
*/
嘗試使用 Timer.Publisher
模擬失敗, 用 dataTaskPublisher示範.
let session = URLSession.shared
let trueURL = URL(string: "https://apple.com")!
let m = session
.dataTaskPublisher(for: trueURL)
.timeout(.timeout(.microseconds(1),
scheduler: RunLoop.main))
.sink(receiveCompletion: {print($0)}, receiveValue: {print($0.count,$1.url)})
/* console
finished
*/