The toObservable array-to-Observable constructor is deprecated.
Creating a cold observable
Instead, use the from statement to create a cold observable:
let stream : Observable<Int> = Observable.from([1,2,3])
Or, if you need the entire array as a record, use the just statement to create an observable cold.
let singleEmissionStream : Observable<[Int]> = Observable.just([1,2,3])
The elements of the array when the from or just statement is called will be the final set of outliers in the onNext events and end with the onCompleted event. Changes to the array will not be recognized as new events for this observed sequence.
This means that if you do not need to listen to changes in this array, you can use the just and from operator to create the observable.
But what if I need to listen for changes in array elements?
To observe changes in the [E] array, you need to use a hot observable , for example, a Variable RxSwift block, as indicated in response to k8mil. You will have an instance of type Variable<[E]> , in which each emission onNext is the current state of the array.
What is the difference between cold and hot observables?
The distinction between cold and hot observables is explained in the RxSwift documentation and in reactivex.io . The following is a brief description of cold observables compared to hot observables.
Cold observables start to run by subscription, i.e. the observed sequence only begins to push values ββto the observers when the Subscribe function is called. [...] This differs from hot observable events, such as mouse movement events or stock tickers, which already produce values ββeven before the subscription is activated.
The from and just statements take the current state of the array when the code runs, thereby completing the set of outliers that it will fire for the observed sequence, regardless of what it is signed for. This is why changes to the set of elements in the array at a later time will not change the set of elements recognized as outliers when creating an observable using the from or just statements.