0

Say I have the following method used to retreive all the "year" from an array of object and then get the distinct value:

public getYears(): Observable<number[]>
{
    return Observable.from(this.payments.map(p => p.year)).distinct().toArray();
}

And elsewhere in my code, I use it like this:

this.getYears().subscribe(years => this.yearsRefinement = years);

Is it enough to do this or is it better to do:

let sub = this.getYears().subscribe(years => this.yearsRefinement = years);
sub.unsubscribe();

I really struggle knowing when I have to unsubcribe or not from an observable. From what I understand, this observable is "finite", so once I get the value, it's completed, so it's not required to unsubscribe from it. Is it correct?

ssougnez
  • 5,315
  • 11
  • 46
  • 79
  • 5
    Possible duplicate of [Do we need to unsubscribe from observable that completes/errors-out?](http://stackoverflow.com/questions/41334931/do-we-need-to-unsubscribe-from-observable-that-completes-errors-out) – cartant Mar 05 '17 at 01:02
  • It seems so. Sorry and thanks ;-) – ssougnez Mar 05 '17 at 12:41

0 Answers0