CreateAsyncObservable not raising OnComplete

…衆ロ難τιáo~ 提交于 2019-12-11 20:47:58

问题


Still playing around with ReactiveUi.

I'm currently having trouble determining when a sequence has ended. I have the following code :

    public RxTest()
    {
        DownloadDocument = ReactiveCommand.CreateAsyncObservable(_ => GetItems().ToObservable());
        DownloadDocument.Subscribe(Console.WriteLine, () => Console.WriteLine("Done"));
    }

    public ReactiveCommand<int> DownloadDocument { get; set; }

    public void RunCommand()
    {
        DownloadDocument.Execute(new object());
    }

    IEnumerable<int> GetItems()
    {
        for (int i = 0; i < 20; i++)
        {
            Thread.Sleep(100);
            yield return i;
        }
    }

The numbers 1 to 19 are printed, but the "Done" does not get printed (ie, OnComplete isn't raised).

The same (similar) code works just fine in pure Rx (I used Linqpad to test this, but also worked when shoe horned into my test application) ...

{
    IObservable<int> range = GetItems().ToObservable(Scheduler.NewThread);
    range.ObserveOn(Scheduler.Default).Subscribe (Console.WriteLine, () => Console.WriteLine("Done"));
}

回答1:


ReactiveCommands never OnComplete because at any point, you could click the button again :) If you want to capture the result of a single invocation of ReactiveCommand, use the ExecuteAsync method.



来源:https://stackoverflow.com/questions/26581227/createasyncobservable-not-raising-oncomplete

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!