我必须确保完成一个 Observable。它正在观察流数据,这不是我要修改的重点。流数据可能根本不打勾。我遇到了一个主题成员,我可以用它来手动完成 observable。
private readonly Subject<bool> _mySubject = new Subject<bool>();
// relevant property that should be completed
public IObservable<myModel> StreamObservable { get; private set; }
IObservable<myModel> someDataObservable = ... ; // may not tick in scenario
// this doesn't work
StreamObservable = someDataObservable
.TakeUntil(_mySubject)
.Finally(() => logger.LogInformation("completed!"));
_mySubject.OnNext(true);
// I tried Amb.. still not going into Finally(), also when someDataObservable doesn't tick
StreamObservable = Observable.Amb(_mySubject.SelectMany(value => Observable.Empty<myModel>()), someDataObservable)
.Finally(() => logger.LogInformation("completed!"));
_mySubject.OnNext(true);
我怎样才能完成,最好使用 Subject.OnNext() ?
主题触发器只是一个想法。我的偏好是留在观察世界并将序列转换为 Observable.Empty(),例如当调用这样的回调时:
private Task<bool> FinishStreamCallback()
{
_mySubject.OnNext(true); // my first approach
return Task.FromResult(true);
}
..targeting 使用 OnCompleted()-订阅另一个类中的相关公共 observable。