No you don't need to destroy a subscription that runs only once as the observable will complete by itself. This is the same if you use take(1)
.
Here is the RxJS doc about first and take.
You have to use them like this:
myOvservable.pipe(first()).subscribe(value => { });
myOvservable.pipe(take(1)).subscribe(value => { });
If you have multiple subscriptions to unsubscribe from, it may be worthwhile to use the takeUntil
trick. You create a subject in which you feed a dummy value when you want your observables to complete:
unsubscribe = new Subject<void>();
myObs1.pipe(takeUntil(this.unsubscribe)).subscribe(value => { });
myObs2.pipe(takeUntil(this.unsubscribe)).subscribe(value => { });
this.unsubscribe.next();
Note: takeUntil
should be the last operator in the pipe sequence in order to avoid leaks (see this article).
You can also use the Subscription
class to manage your subscriptions:
subscriptions = new Subscription();
this.subscriptions.add(myObs1.subscribe(value => { }));
this.subscriptions.add(myObs2.subscribe(value => { }));
this.subscriptions.unsubscribe();