Won't Fix
Last Updated: 13 Nov 2024 16:59 by ADMIN
AscanioTziazas
Created on: 29 Nov 2022 08:17
Category: ListView
Type: Feature Request
1
ListView: LoadOnDemandCollection should accept an asynchronous callback returning a Task

Currently the LoadOnDemandCollection accepts a callback of the following format in the constructor:

public LoadOnDemandCollection(Func<CancellationToken, IEnumerable> action)

It is a very common scenario to populate the items asynchronously. In its current form the collection would require blocking the current thread to populate the results:

ItemsSource = new ListViewLoadOnDemandCollection((cancelationToken) =>
{
    var result = new List<ItemsModel>();
    try
    {
        var items = dataService.GetItemsAsync().Result;

        // TODO: Handle the result.

        return result;
    }
    catch (Exception e)
    {
        // TODO: Handle the exception.
        return null;
    }
});

This is not desired, as using Task.Result blocks the current thread and is considered an anti-pattern, in general.

A better approach would be to add a second overload of the constructor, allowing asynchronous calls:

public LoadOnDemandCollection(Func<CancellationToken, Task<IEnumerable>> action)

This way we can use async and await in the callback instead:

ItemsSource = new ListViewLoadOnDemandCollection(async (cancelationToken) =>
{
    var result = new List<ItemsModel>();
    try
    {
        var items = await dataService.GetItemsAsync();

        // TODO: Handle the result.

        return result;
    }
    catch (Exception e)
    {
        // TODO: Handle the exception.
        return null;
    }
});

According to my tests, the first blocking approach is not a problem, as the ListViewLoadOnDemandCollection starts a thread internally. That behavior is not obvious however, and using Task.Result is somewhat counterintuitive, so the second approach is much better from the user's perspective.

1 comment
ADMIN
Didi
Posted on: 13 Nov 2024 16:59

Hello,

The status of this item is set to Won't fix as the RadListView control is now obsolete and will be removed in the future. Use the RadCollectionView control instead. All votes are migrated to the newly created item for the RadCollectionView: CollectionView: LoadOnDemandCollection should accept an asynchronous callback returning a Task

The RadCollectionView is a complete, ground-up rewrite of the ListView. The RadCollectionView offers improved performance, enhanced features, and a modernized approach to managing lists of data. The RadCollectionView incorporates all of the ListView's key features. More about the differences between both components and how to migrate to the new RadCollectionView is available in the Migrating the Telerik .NET MAUI RadListView to RadCollectionView article.

Regards,
Didi
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.