StephenCleary / Mvvm

MVVM helpers, including calculated properties and asynchronous notification tasks.
MIT License
134 stars 38 forks source link

NotifyTask - delay in loading data #16

Closed pmccowat closed 7 years ago

pmccowat commented 7 years ago

Hi Stephen,

I am using NotifyTask to load data in a viewmodel constructor in a WPF project but it is taking a few seconds for the data to show on the screen. It is the same using AsyncEx and possibly related to http://stackoverflow.com/questions/29058130/why-does-this-async-method-still-work-on-ui-thread-and-slow-navigation

My viewmodel is basically the code below and I bind the datacontext of a grid to Test.Result.

public class TestViewModel : ViewModelBase
    {
        private readonly ITestService _TestService;
        private int _Id;
        NotifyTask<TestDto> _Test;
        public int Id
        {
            get
            {
                return _Id;
            }

            set
            {
                if (_Id == value)
                {
                    return;
                }

                _Id = value;
                RaisePropertyChanged(() => Id);
            }
        }

        public NotifyTask<TestDto> Test
        {
            get
            {
                return _Test;
            }
            set
            {
                if (_Test == value)
                {
                    return;
                }
                _Test = value;
                RaisePropertyChanged(() => Test);
            }
        }

        public TestViewModel(int id)
        {
            _Id = id;
            _TestService = ServiceLocator.Current.GetInstance<ITestService>();
            Test = NotifyTask.Create(_TestService.GetTest(_Id));
        }

    }
StephenCleary commented 7 years ago

There's no delay in NotifyTask.

However, if you are binding tons of data (like thousands of records) all at once, then the data binding will take time. The only way to avoid this is to use data virtualization.

If this doesn't address your problem, then please post a minimal, reproducible example and I'll take a closer look.

pmccowat commented 7 years ago

Thanks Stephen, this is for one record, the other call is to a list of 12 records which is about the same or quicker. I will look at the WPF code and get an example if it continues to be slow.

StephenCleary commented 7 years ago

Yeah, it should be really fast, then. I mean, unless your DB is slow or something.

pmccowat commented 7 years ago

No, it's pretty quick - well indexed small table on a fast Azure instance. Only slows when I use NotifyTask to call the data. I normally use your CreateAsync factory idea to construct a viewmodel but I wanted to use the empty constructor here for so looked at the NotifyTask to bind data.

StephenCleary commented 7 years ago

That's odd. All NotifyTask does is call INotifyPropertyChanged when the task completes.

pmccowat commented 7 years ago

Stephen, apologies - it was a previously quick API call that slowed down the same time as I moved to NotifyAsync.

StephenCleary commented 7 years ago

Thanks for letting me know!