We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Re-request data fetch if first attempt is unsuccessful

Hello. Please tell me how to correctly implement the repeated operation of the command to receive data if the first attempt was not successful and the SfListView does not have previously loaded data.

Problem example:

Opening the MainPage

The command to load data is run, but an error occurs(for example no internet connection)

The SfListView has no data because it was not loaded in the previous step

Perhaps there is a parameter responsible for re-requesting data until the first data is loaded?

Unfortunately, the automatic start of the command occurs only if the screen orientation changes (vertical-horizontal)

Perhaps there is a property responsible for re-running the command until the first data is successfully received?


<sflv:SfListView x:Name="listView"

ItemsSource="{Binding Items}"

ItemSize="{OnPlatform Android={OnIdiom Phone=170, Tablet=280}}"

AutoFitMode="Height"

LoadMoreOption="Auto"

LoadMoreCommand="{Binding LoadSeriesCommand}">


public class SeriesViewModel

{

public ICommand LoadSeriesCommand => new Command(async () => await LoadSeriesAsync());


async Task LoadSeriesAsync()

{

if (IsBusy)

return;

try

{

IsBusy = true;


var series = await _seriesService.GetSeries();

foreach (var item in series)

Items.Add(item.Map());

}

catch (Exception ex)

{

Debug.WriteLine($"Unable to get series {ex.Message}");

await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Ok");

}

finally

{

IsBusy = false;

}

}


4 Replies

SY Suthi Yuvaraj Syncfusion Team March 27, 2023 02:00 PM UTC

Hi Oct,


We would like to let you know that, you can load the data at runtime in LoadMoreCommand , by setting the CanExecute method based on the required criteria, you can check the condition where you want to load more the items or not on the
CanExecute method in LoadMoreCommand , Please refer the below code snippet and documentation for more reference.


Code snippet:

In ViewModel.cs

 

LoadMoreItemsCommand = new Command<object>(LoadMoreItems, CanLoadMoreItems);

 

private bool CanLoadMoreItems(object obj)

{

    if (Products.Count >= totalItems)

        return false;

    return true;

}

 


UG Link: https://help.syncfusion.com/maui/listview/loadmore?cs-save-lang=1&cs-lang=csharp#load-more-automatically


Regards,

Suthi Yuvaraj



OC Oct replied to Suthi Yuvaraj March 27, 2023 04:53 PM UTC

Thanks. Unfortunately, in this variant, the method call also occurs 1 time. If the first method call was not successful, then the second time the method is not run either.


step1. Open page

step2. SfListView init command

step3. execute CanLoadMoreItems(return true)

step4. execute LoadSeriesAsync(error has occurred) Items.Count=0

step5. I want the ' LoadSeriesCommand ' command to be called again.

Of course, I can execute the command again in the finaly block in case of an error, but this option seems bad to me.

finally

{

IsBusy = false;


if(Items.Count = 0){

LoadSeriesCommand.Execute();

}

}




public ICommand LoadSeriesCommand => new Command(LoadSeriesAsync, CanLoadMoreItems);

private bool CanLoadMoreItems(object obj) {

if (Items.Count >= 0)

return true;

return false;

}

async void LoadSeriesAsync(object obj)

{

if (IsBusy)

return;


try

{

IsBusy = true;


var series = await _seriesService.GetSeries();

//Add mapper

if (Items.Count != 0)

{

Items.Clear();

return;

}


foreach (var item in series)

Items.Add(item.Map());


}

catch (Exception ex)

{

Debug.WriteLine($"Unable to get series {ex.Message}");

await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Ok");

}

finally

{

IsBusy = false;

}

}



SY Suthi Yuvaraj Syncfusion Team March 29, 2023 02:33 AM UTC

Oct,


We would like to inform you that there is no need to manually call the LoadMoreCommand since it is designed to execute automatically until the CanExecute method returns false.

We have tested the code snippet you provided and noticed that the items collection is being cleared inside the LoadMoreCommand, which causes each item in the collection to be cleared. We have modified the sample as per your requirements.

Please take a look at the updated sample and let us know if you have any concerns.


Attachment: ListViewMaui_8997d110.zip


OC Oct March 29, 2023 09:20 AM UTC

Suthi, thanks a lot 


Loader.
Up arrow icon