0

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"));
}
enomam
  • 89
  • 5

1 Answers1

0

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.

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • But now that wraps a command with another command ... `ExecuteDownload = ReactiveCommand.CreateAsyncTask(Observable.Return(true), async _ => { var x = await DownloadDocument.ExecuteAsync(); CompletionState = CompletionState.Success; return x; });` Which is the sort of code I wanted to avoid ... (and sorry, have no idea how to get formatting correct in a comment ...) – enomam Oct 28 '14 at 05:14
  • Also, with the above, I need to subscribe to ThrownExceptions on both commands for them to work. – enomam Oct 28 '14 at 05:32