I'm new to Combine and I'd like to get a seemingly simple thing. Let's say I have a collection of integer, such as:
let myCollection = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
I'd like to publish each element with a delay of, for example, 0.5 seconds.
print 0
wait for 0.5secs
print 1
wait for 0.5secs
and so forth
I can easily get the sequence publisher and print the elements like this:
let publisherCanc = myCollection.publisher.sink { value in
print(value)
}
But in this case all the values are printed immediately. How can I print the values with a delay? In Combine there's a .delay
modififer, but it's not for what I need (indeed, .delay
delays the entire stream and not the single elements). If I try:
let publisherCanc = myCollection.publisher.delay(for: .seconds(0.5), scheduler: RunLoop.main).sink { value in
print(value)
}
All I get it's just an "initial" delay, then the elements are printed immediately.
Thanks for your help.