CreateAsyncObservable not raising OnComplete

2019-09-06 21:01发布

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"));
}

标签: c# reactiveui
1条回答
2楼-- · 2019-09-06 21:31

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.

查看更多
登录 后发表回答