This is a migrated thread and some comments may be shown as answers.

LoadOnDemand MVVM with async data

6 Answers 535 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 28 Feb 2018, 10:26 PM
     I'm trying to setup a listview with Automatic ondemand loading, but my data source(s) are using async data calls. I've been unable to get my ListViewLoadOnDemandCollection to either initial load async data or to then call its ondemand service. There's nothing in the documentation that I've been able to find that has helped.  Thanks.

6 Answers, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 28 Feb 2018, 11:38 PM
Hello Andrew,

For this scenario, take a look at my ComicVine demo on GitHub for inspiration. It uses Automatic Load On Demand with an async HttpClient call to an API to get the next set of items. You can find the relevant code in the CharactersViewModel class and the RadListView is in CharactersPage.

If you have any implementation specific issues, open a support ticket here and share your code so that we can investigate further (you'll need have manager assign you as a Licensed User in this portal because right now you're using the product unlicensed).

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items

[Edited July 2019 - fixed GitHub hyperlinks and updated description]
0
Andrew
Top achievements
Rank 1
answered on 02 Mar 2018, 02:46 PM

Lance,

I was able to successfully get it working using the ObservableCollection and the load command in the OnAppearing override, so thanks much for that, it was a big help!

One minor thing I haven't been able to resolve is that the second set of data requested gets doubled. I added filtering to get it working for now, but it looks like the CurrentItemCount hasn't been updated to reflect that a second call has been made, so data service requests 2 and 3 both send the same CurrentItemCount out. Right now I'm only loading 4 items at a time, is that part of why its getting triggered so close together?

 

public async Task GetArchiveItemsAsync()
        {
            try
            {
                IsBusy = true;
 
                var items = await _newsroomDataService.GetSeyExpArchiveArticlesList(CurrentItemCount);
                CurrentItemCount += items.Count;
 
                foreach (var item in items)
                {
                    //fix for it double loading the second set, need to find out why
                    var existingItem = ArticlesArchive.Where(x => x.ID == item.ID).FirstOrDefault();
                    if (existingItem == null)
                    {
                        ArticlesArchive.Add(item);
                    }
                }
            }
            catch (Exception e)
            {
                ReportingService.LogAppError(e);
            }
            finally
            {
                IsBusy = false;
                IsLoadOnDemandActive = false;
            }
        }
0
Lance | Manager Technical Support
Telerik team
answered on 02 Mar 2018, 03:11 PM
Hi Andrew,

Yes, 4 items is likely the reason from what I can see in your code, I have a solution for you but first let me explain why it's as issue and then you can prepare for it.

The control has a property named LoadOnDemandBufferItemsCount, for which the default value is 20. This property's value tells the mechanism when to fetch the next set of items, when the list reaches that number of items away from the bottom of the current list, it will perform a fetch.

For example, if the list currently has 60 items in it and the user scrolls to item 40, the RadListView will trigger an ItemsRequested.

In your case, since you only have 4 items at a time, it's going to rapidly fetch items until the user is 20 items away from the bottom.

Solution

You can do two things to help:

1 - Set the LoadOnDemandBufferItemsCount to a lower number that works for you, say 4.
2 -  Add a protection mechanism to the beginning of your Task call to prevent the method from getting more items until you're ready for them.

An example of #2 would be

public async Task GetArchiveItemsAsync()
{
    if (IsBusy)
        return;
 
    try
    {
        IsBusy = true;
 
        var items = await _newsroomDataService.GetSeyExpArchiveArticlesList(CurrentItemCount);
        CurrentItemCount += items.Count;
 
        foreach (var item in items)
        {
            //fix for it double loading the second set, need to find out why
            var existingItem = ArticlesArchive.Where(x => x.ID == item.ID).FirstOrDefault();
            if (existingItem == null)
            {
                ArticlesArchive.Add(item);
            }
        }
    }
    catch (Exception e)
    {
        ReportingService.LogAppError(e);
    }
    finally
    {
        IsBusy = false;
        IsLoadOnDemandActive = false;
    }
}


Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Andrew
Top achievements
Rank 1
answered on 02 Mar 2018, 04:41 PM
The LoadOnDemandBufferItemsCount did the trick. Thanks again for the help!
0
xiaoyu
Top achievements
Rank 1
answered on 18 Jul 2019, 07:37 AM
I also encountered the same problem, but the example link you provided is invalid, how can I get the solution?(https://github.com/LanceMcCarthy/ComicVine/blob/master/ComicVine/ComicVine/Portable/ViewModels/CharactersViewModel.cs)
0
Lance | Manager Technical Support
Telerik team
answered on 18 Jul 2019, 03:25 PM
Hello,

Thank you for letting me know that the link no longer works, I'll edit and fix them as soon as possible. Until then, you can go to the root of the project repo:

https://github.com/LanceMcCarthy/ComicVine 

and then drill your way down to the view model: 

https://github.com/LanceMcCarthy/ComicVine/blob/master/ComicVine/ComicVine.Forms/ViewModels/CharactersViewModel.cs

If you have any trouble implementing LoadOnDemand, feel free to open a Support Ticket and attach your code so that we can directly investigate.

Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
ListView
Asked by
Andrew
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Andrew
Top achievements
Rank 1
xiaoyu
Top achievements
Rank 1
Share this question
or