现在开始启动的计时器发布者
Timer publisher with initial fire now
我有一个简单的定时器发布器,每 10 秒触发一次。
Timer
.publish(every: 10, on: .main, in: .common)
.autoconnect()
.map { _ in ... }
.sink(receiveValue: { [weak self] in
...
})
.store(in: &subscriptions)
但是,它第一次触发是在 10 秒后。我可以将它设置为触发第一个值 now?
我能够实现我想要的,但我不得不添加另一个发布者:
let timer = Timer
.publish(every: 10, on: .main, in: .common)
.autoconnect()
let initial = Just(Date.init())
timer.merge(with: initial)
.map { _ in ... }
.sink(receiveValue: { [weak self] in
...
})
.store(in: &subscriptions)
或者创建另一个发布者,您可以简单地prepend
到您的计时器发布者:
Timer
.publish(every: 10, on: .main, in: .common)
.autoconnect()
.prepend(Date())
.map { _ in ... }
.sink(receiveValue: { [weak self] in
...
})
.store(in: &subscriptions)
以上代码的效果是一样的,立即发布一个值,让定时器发布其他值。
我有一个简单的定时器发布器,每 10 秒触发一次。
Timer
.publish(every: 10, on: .main, in: .common)
.autoconnect()
.map { _ in ... }
.sink(receiveValue: { [weak self] in
...
})
.store(in: &subscriptions)
但是,它第一次触发是在 10 秒后。我可以将它设置为触发第一个值 now?
我能够实现我想要的,但我不得不添加另一个发布者:
let timer = Timer
.publish(every: 10, on: .main, in: .common)
.autoconnect()
let initial = Just(Date.init())
timer.merge(with: initial)
.map { _ in ... }
.sink(receiveValue: { [weak self] in
...
})
.store(in: &subscriptions)
或者创建另一个发布者,您可以简单地prepend
到您的计时器发布者:
Timer
.publish(every: 10, on: .main, in: .common)
.autoconnect()
.prepend(Date())
.map { _ in ... }
.sink(receiveValue: { [weak self] in
...
})
.store(in: &subscriptions)
以上代码的效果是一样的,立即发布一个值,让定时器发布其他值。