If I use the ToEnumerable
extension on an IObservable
, there is a chance the user will not iterate all of the elements. In that case how can the IDisposable
declared in Observable.Create
be properly disposed?
For the sake of argument let's say that it is not an option to directly return the IObservable
to the user (in which case the user could implement cancellation themselves).
private IObservable<Object> MakeObservable()
{
return Observable.Create(async (observer, cancelToken) =>
{
using(SomeDisposable somedisposable = new SomeDisposable())
{
while(true)
{
Object result = somedisposable.GetNextObject();
if(result == null)
{
break;
}
observer.OnNext(result);
}
}
}
}
public IEnumerable<Object> GetObjects()
{
return MakeObservable().ToEnumerable();
}
public void Test()
{
IEnumerable<Object> e = GetObjects();
int i = 0;
foreach(Object o in e)
{
if(i++ == 10)
break;
}
//somedisposable is not disposed here!!!
}