Winston Du
Winston Du

Reputation: 390

RxSwift: have a relay accept items in succession from an array

Let's say I have an array of Ints

let array = [1, 2, 3, 4, 5]

And let's say I also have a publish Relay that accepts Int elements

let relay = PublishRelay<Int>()

Is there a better way than to for loop over my array to stuff them into the relay?

for num in array {
   relay.accept(num)
}

Upvotes: 1

Views: 379

Answers (1)

Daniel T.
Daniel T.

Reputation: 33979

The Observable.from operator is what you are looking for:

Observable.from(array)
    .bind(to: relay)

Upvotes: 2

Related Questions