LoadOnDemand MVVM with async data

7 posts, 1 answers
  1. Andrew
    Andrew avatar
    7 posts
    Member since:
    Dec 2012

    Posted 28 Feb 2018 Link to this post

         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.
  2. Answer
    Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1168 posts

    Posted 28 Feb 2018 Link to this post

    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]
  3. Andrew
    Andrew avatar
    7 posts
    Member since:
    Dec 2012

    Posted 02 Mar 2018 Link to this post

    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;
                }
            }
  4. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1168 posts

    Posted 02 Mar 2018 Link to this post

    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
  5. Andrew
    Andrew avatar
    7 posts
    Member since:
    Dec 2012

    Posted 02 Mar 2018 Link to this post

    The LoadOnDemandBufferItemsCount did the trick. Thanks again for the help!
  6. xiaoyu
    xiaoyu avatar
    1 posts
    Member since:
    Jul 2019

    Posted 18 Jul 2019 in reply to Lance | Manager Technical Support Link to this post

    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)
  7. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1168 posts

    Posted 18 Jul 2019 Link to this post

    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
Back to Top