1
votes

In my project, we are manipulating and store data in services with array and Observables. We create the observables like this :

Observable.of(array)

and then we only update the array when we add/remove/etc element.

In our component, we only consume the observable and use the async filter.

I was wondering if it's good practice because even in the documentation of Angular, they recommend to use combination of Observable and Subject, not Observable and Array.

Why using Subject is better ? Is there a difference in the performance ?

Thanks

1
Your question is more likely to be answered if you show what you are doing with code, rather than explain what you are doing with words. - cartant

1 Answers

0
votes

Observable and Subject are different. I don't see a benefit in casting your arrays to observables just so that you can use the async pipe because it will work just fine if you use the array directly in the template without async pipe and without casting it to an observable.

A subject by definition is both an observable and an observer, meaning that you can both read and write values to it. An observable you will only be able to read values from. What you are doing with Observable.of() is that you're creating a new observable each time.

With a subject, you can pass it/emit new values by using mySubject.next('new value') and you can then subscribe to the values just like you would an observable with mySubject.subscribe().

So one is not better than the other, they are just for different purposes.