I am trying to determining what to do with the following code
let sub = myObservable.subscribe(
v => doThing(v),
e => handle(e),
() => sub.unsubscribe(),
)
The issue is 1. This code is incorrect because of myObservable completed synchronously, an NPE would be thrown on completion. 2. Even though I suspect that the unsubscribe call here is good practice. I can't help but feel it might not be necessary because I have not see it done anywhere else.
I have read this article https://blog.angularindepth.com/why-you-have-to-unsubscribe-from-observable-92502d5639d0 but it actually leaves me more confused than when I started.
If I do
let subA = myObservable.pipe(take(1)).subscribe()
let subB = myObservable.pipe(takeUntil(foo)).subscribe()
Do I not need to unsubscribe subA and subB anymore?
how about subC over here?
let subC = myObservable.pipe(finalize(() => cleanupOtherResources())).subscribe()
Or do I have to add all subscription into a list in every class that calls subscribe()
on any BehaviorSubject
and unsubscribe them at once?
Thanks!