Gayan Buddhika
Gayan Buddhika

Reputation: 43

what is the proper way of subscribing to a ReactiveCommand in ReactiveUI 8.2

I have following snippet in ViewModel.

 public ReactiveCommand<object, System.Reactive.Unit> LoadCustomerDetails;
ReactiveCommand<OrderViewPager<SalesOrderOrderOptionsEnum>, CommandSubmitResultDto<List<SalesOrderDto>>> _loadSalesOrderList;
public ReactiveCommand<OrderViewPager<SalesOrderOrderOptionsEnum>, CommandSubmitResultDto<List<SalesOrderDto>>> LoadSalesOrderList
{
    get { return _loadSalesOrderList; }
    private set { this.RaiseAndSetIfChanged(ref _loadSalesOrderList, value); }
}

 this.LoadSalesOrderList = ReactiveCommand.CreateFromTask<Pager<OrderOptionsEnum>, CommandSubmitResultDto<List<SalesOrderDto>>>(
                async filter =>
                {
                    Debug.WriteLine("Load SalesOrderList...");
                    Debug.WriteLine("Customer Id : " + SelectedCustomerId);
                    await LoadCustomerDetails.Execute();
                    var result = await SalesOrderMobApi.GetByCustomerTraderEntityIdPaged(SelectedCustomerId, filter, null, SalesOrderTypeEnum.SalesOrder, SalesOrderPOOptions.NotOriginatingFromPurchaseOrder);

                    return result;
                })
                .DisposeWith(ViewModelBindings.Value);

            this.LoadSalesOrderList.ThrownExceptions
                .Subscribe(ex =>
                {
                    Debug.WriteLine("Load SalesOrderList Failed!");
                });

            this.LoadSalesOrderList
               .ObserveOn(RxApp.MainThreadScheduler)
               .Subscribe(result =>
               {
                   if (result.PagingInfo.CurrentPage > 1)
                   {
                       foreach (var item in result.Data)
                       {
                           SalesOrdersList.Add(SalesOrderVMM.From(item));
                       }
                   }
                   else
                   {
                       SalesOrdersList.Clear();
                       foreach (var item in result.Data)
                       {
                           SalesOrdersList.Add(SalesOrderVMM.From(item));
                       }
                   }
               });

            LoadCustomerDetails = ReactiveCommand.CreateFromTask<object, System.Reactive.Unit>(
                async _ =>
                {
                    Debug.WriteLine(SelectedCustomerId);
                    var customers = await TraderEntityMobApi.GetById(SelectedCustomerId);
                    var customer = customers.Data;
                    SelectedCustomer = customer;
                    return System.Reactive.Unit.Default;
                }
                ).DisposeWith(ViewModelBindings.Value);

It sometimes gives exception as follows.

    System.NullReferenceException: Object reference not set to an instance of an object.
    06-20 16:05:02.480 I/MonoDroid(15304):   at DistributrIII.Mobile.Lib.VM.SalesOrder.CreateSOListVM.<RegisterObservables>b__43_2 (System.Collections.Generic.List`1[T] result) [0x0000e] in C:\Users\gayanbu\Source\Repos\Distributr 3.0 UI\Mobile\DistributrIII.Mobile.Lib\VM\SalesOrder\CreateSOListVM.cs:131 .at System.Reactive.AnonymousSafeObserver`1[T].OnNext (T value) [0x0000a] in <99f8205c51c44bb480747b577b8001ff>:0 
06-20 16:05:02.480 I/MonoDroid(15304):   at System.Reactive.ScheduledObserver`1[T].Run (System.Object state, System.Action`1[T] recurse) [0x000f5] in <99f8205c51c44bb480747b577b8001ff>:0 
06-20 16:05:02.480 I/MonoDroid(15304):   at System.Reactive.Concurrency.Scheduler+<>c__DisplayClass49_0`1[TState].<InvokeRec1>b__0 (TState state1) [0x0001e] in <99f8205c51c44bb480747b577b8001ff>:0 
06-20 16:05:02.480 I/MonoDroid(15304):   at System.Reactive.Concurrency.Scheduler.InvokeRec1[TState] (System.Reactive.Concurrency.IScheduler scheduler, 

I guess it tries to execute the code inside reactive command ,LoadSalesOrderList even the result of this is null. How to handle this ? Could someone kindly explain the proper way of subscribing to Reactive Command. I am executing this command in the page load as, this.ViewModel.LoadSalesOrderList.Execute().subscribe(new Pager<OrderOptionsEnum>()) Thanks!

Upvotes: 0

Views: 621

Answers (1)

albilaga
albilaga

Reputation: 439

if you want your command when throw exception to be catched in ThrownExceptions execute command like Observable.Return(input).InvokeCommand(Command).DisposeWith(disposable) where input is the input for command and Command is the name of the Command

Upvotes: 0

Related Questions