RxSwift以时间间隔发出起始值并对其进行修改
我需要每秒钟减少一次输入值,然后打印它。 当该值达到0时,它将变为100,递减过程将重复,并再次进行 例如:RxSwift以时间间隔发出起始值并对其进行修改,swift,timer,observable,rx-swift,Swift,Timer,Observable,Rx Swift,我需要每秒钟减少一次输入值,然后打印它。 当该值达到0时,它将变为100,递减过程将重复,并再次进行 例如: Given value: -> 234 Timer starts decreasing with every second -> 233 -> 232 ... -> 1 -> 0 and the whole process repeats, but starts with value 100 (and again decreasing, re
Given value:
-> 234
Timer starts decreasing with every second
-> 233
-> 232
...
-> 1
-> 0 and the whole process repeats,
but starts with value 100
(and again decreasing, reaches 0, starting from 100)
我知道如何在Rx中使用定时器,但如何将其与所述情况连接?您只需要一个初始值如下的变量:
var start = 234
func startTimer() {
_ = Observable<Int>
.timer(0, period: 1, scheduler: MainScheduler.instance)
.subscribe(onNext: { _ in
self.start -= 1
if self.start <= 0 {
self.start = 100
}
print("Timer valuse>>>> : ",self.start)
})
}
var start=234
func startTimer(){
_=可见
.timer(0,周期:1,计划程序:MainScheduler.instance)
.subscribe(onNext:{uu}in
self.start-=1
如果self.start您只需要一个初始值如下的变量:
var start = 234
func startTimer() {
_ = Observable<Int>
.timer(0, period: 1, scheduler: MainScheduler.instance)
.subscribe(onNext: { _ in
self.start -= 1
if self.start <= 0 {
self.start = 100
}
print("Timer valuse>>>> : ",self.start)
})
}
var start=234
func startTimer(){
_=可见
.timer(0,周期:1,计划程序:MainScheduler.instance)
.subscribe(onNext:{uu}in
self.start-=1
如果self.start从1秒间隔创建一个可观测值,从数字序列创建另一个可观测值,然后将它们压缩在一起,怎么样
let interval = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
let startValue1 = 234
let startValue2 = 100
let range1 = Observable
.range(start: 0, count: startValue1 + 1)
.map { startValue1 - $0 }
let range2 = Observable
.range(start: 0, count: startValue2 + 1)
.map { startValue2 - $0 }
.repeatWithBehavior(.immediate(maxCount: .max))
.subscribeOn(SerialDispatchQueueScheduler(qos: .background))
Observable.zip(
interval,
range1.concat(range2))
.subscribe(onNext : { (_, remainingTime) in
print("\(remainingTime)")
})
let interval=Observable.interval(1,调度程序:MainScheduler.instance)
设startValue 1=234
让startValue2=100
设range1=可观测
.范围(起始值:0,计数:起始值1+1)
.map{startValue 1-$0}
设range2=可观测
.范围(起始值:0,计数:起始值2+1)
.map{startValue 2-$0}
.repeatWithBehavior(.immediate(最大计数:.max))
.subscribeOn(SerialDispatchQueueScheduler(qos:.后台))
可观察的.zip(
间隔
范围1.concat(范围2))
.subscribe(onNext:{(u,remainingTime)在
打印(“\(剩余时间)”)
})
这有点冗长,但它避免了任何可变状态。HTH如何从1秒间隔创建一个可观测值,从数字序列创建另一个可观测值,然后将它们压缩在一起?如下所示:
let interval = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
let startValue1 = 234
let startValue2 = 100
let range1 = Observable
.range(start: 0, count: startValue1 + 1)
.map { startValue1 - $0 }
let range2 = Observable
.range(start: 0, count: startValue2 + 1)
.map { startValue2 - $0 }
.repeatWithBehavior(.immediate(maxCount: .max))
.subscribeOn(SerialDispatchQueueScheduler(qos: .background))
Observable.zip(
interval,
range1.concat(range2))
.subscribe(onNext : { (_, remainingTime) in
print("\(remainingTime)")
})
let interval=Observable.interval(1,调度程序:MainScheduler.instance)
设startValue 1=234
让startValue2=100
设range1=可观测
.范围(起始值:0,计数:起始值1+1)
.map{startValue 1-$0}
设range2=可观测
.范围(起始值:0,计数:起始值2+1)
.map{startValue 2-$0}
.repeatWithBehavior(.immediate(最大计数:.max))
.subscribeOn(SerialDispatchQueueScheduler(qos:.后台))
可观察的.zip(
间隔
范围1.concat(范围2))
.subscribe(onNext:{(u,remainingTime)在
打印(“\(剩余时间)”)
})
这有点冗长,但它避免了任何可变状态。HTH这是我一直在寻找的东西,谢谢:)这是我一直在寻找的东西,谢谢:)