将 RxSwift 计时器和间隔可观察量组合成一个带有完成的可观察量
Combine RxSwift timer and interval observables into a single observable with completion
我想创建一个行为类似这样的可观察对象。
var count = 0
func setupCountdownTimer() {
let rx_countdownTimer = CountdownTimer.observable(5)
rx_countdownTimer >- subscribeNext {
secondsRemaining in
println(secondsRemaining) // prints 5, then 4, 3, 2, 1, then finally 0
count = secondsRemaining
}
rx_countdownTimer >- subscribeCompleted {
println(count) // prints 5, assuming countdownTimer stopped 'naturally'
}
}
@IBAction func stop(sender: UIButton) {
rx_countdownTimer.sendCompleted() // Causes 2nd println above to output, say, 3, if that's how many seconds had elapsed thus far.
}
似乎我应该能够在这里以某种方式组合 timer observable and an interval observable,但我似乎无法找出执行此操作的正确策略。 Rx 的新手,所以我对我做错事的可能性持开放态度。 ¯\_(ツ)_/¯
是这样吗?
var timer = CountdownTimer(5)
var count = 0
func setupCountdownTimer() {
timer.observable >- subscribeNext { n in
println(n) // "5", "4", ..., "0"
self.count = n
}
timer.observable >- subscribeCompleted {
println(self.count)
}
}
@IBAction func stop(sender: UIButton) {
timer.sendCompleted()
}
我想创建一个行为类似这样的可观察对象。
var count = 0
func setupCountdownTimer() {
let rx_countdownTimer = CountdownTimer.observable(5)
rx_countdownTimer >- subscribeNext {
secondsRemaining in
println(secondsRemaining) // prints 5, then 4, 3, 2, 1, then finally 0
count = secondsRemaining
}
rx_countdownTimer >- subscribeCompleted {
println(count) // prints 5, assuming countdownTimer stopped 'naturally'
}
}
@IBAction func stop(sender: UIButton) {
rx_countdownTimer.sendCompleted() // Causes 2nd println above to output, say, 3, if that's how many seconds had elapsed thus far.
}
似乎我应该能够在这里以某种方式组合 timer observable and an interval observable,但我似乎无法找出执行此操作的正确策略。 Rx 的新手,所以我对我做错事的可能性持开放态度。 ¯\_(ツ)_/¯
是这样吗?
var timer = CountdownTimer(5)
var count = 0
func setupCountdownTimer() {
timer.observable >- subscribeNext { n in
println(n) // "5", "4", ..., "0"
self.count = n
}
timer.observable >- subscribeCompleted {
println(self.count)
}
}
@IBAction func stop(sender: UIButton) {
timer.sendCompleted()
}