Does CreateFromObservable works asynchrony?

I'm trying use ReactiveUI 7.4 in WPF project, and I think it's great framework. But it causes great difficulties in studying the absence, or the outdated documentation. In doc https://docs.reactiveui.net/en/user-guide/commands/asynchronous-synchronous.html says so CreateFromObservable is asynchrony, but in my example it's run syncronly.

RefreshList = ReactiveCommand.CreateFromObservable<ReactiveList<ClientDto>>(
                        () => Observable
                            .Return(_clientsService.GetClientsList())

and latter

   _isBusy = this.WhenAnyObservable(x => x.RefreshList.IsExecuting)
            .ToProperty(this, vm => vm.IsBusy);

when i do InvokeCommand method runs syncronly, and IsExecuting observe only after GetClientsList() completed (change to false and after to true). But variand with task works:

RefreshList = ReactiveCommand.CreateFromTask(async _ =>
            {
                return await Task.Run(() => _clientsService.GetClientsList());
            }
        );

Is it bug? Or changes in framework? PS I also trying plays with SubscribeOn and ObservableOn but nothing helps (((.

1 answer

  • answered 2017-06-17 20:04 Jon Stødle

    Observable.Return() does it's work on the current thread, which means it's blocking. In your case the current thread is the UI thread.

    You can specify a scheduler, but that only affects where the value is returned, not where it's produced.

    I've written about Observable.Return() and how it behaves in this blog post.

    It looks like _clientsService.GetClientsList() is implemented synchronously. To make it asynchronous you can move the work to the task pool. You've already done this by running it in a Task. It's also possible to use `Observable.Start()´:

    RefreshList = ReactiveCommand.CreateFromObservable<ReactiveList<ClientDto>>(
                            () => Observable
                                .Start(_clientsService.GetClientsList(), RxApp.TaskpoolScheduler);