I'm trying to understand when is a good time to return a disposable in the function passed to Observable.Create
vs just disposing any resources through scope by a using
statement.
Is returning the disposable more for cases where the Observable
is an infinite stream? Even if so I don't understand how the using
block won't still dispose the resource even if the stream is closed prematurely
Thanks to everyone responding in this post it's helped me understand a bit better. I've had a lot of stumbling in my understanding of RX and I think a lot of this just comes down to limited documentation and seems like many people online don't quite understand perfectly either so there's a lot of misinformation to sort through.
This other answer does the trick for me https://stackoverflow.com/a/7707768/7183974 .
What it really comes down to is for when we have non-blocking code in our Observable.Create
method. So when our observable is subscribed to we instantly return a disposable that can clean up any asynchronous / concurrent processes in the event that we need to cancel a subscription early.
This is necessary for cases where maybe your observable is using other async (push-based) code.
For iterative (pull-based) code that you simply want to be push based then you can use Observable.Create
but TBH I think just using an Iterator is better and if you need it to be a push-based API then just use ToObservable
.
I was trying to implement a push-based iterator so the disposable seemed redundant to me which is what confused me. I've since refactored my code to be pull-based and if I ever were to need it to be push-based again I would just use ToObservable
.