0
votes

I need to show very big data in a ListView. Must I load all the data once? My code is below:

using System.Collections.ObjectModel;

namespace Brunie.Mobile.ViewModels {
    public class VmListCompany {
        public ObservableCollection<Company> AllCompanies {
            get;
        } = new ObservableCollection<Company>();
        public VmListCompany() {
            LoadAllCompanies();
        }
        private void LoadAllCompanies() {
            Company company = null;
            while(null != (company = GetNextCompany(company))) {
                AllCompanies.Add(company);
            }
        }
        private bool HasNextCompany(Company company) {
            bool hasNextCompany = false;
            ......
            return (hasNextCompany);
        }
        private Company GetNextCompany(Company company) {
            if(!HasNextCompany(company)) {
                return (null);
            }
            Company nextCompany = new Company();
            ......
            return (nextCompany);
        }
    }
    public class Company {
        public string Name {
            get;
            set;
        }
        public string Address {
            get;
            set;
        }
        public int NumberOfEmployees {
            get;
            set;
        }
    }
}

ListView in Xaml:

<ListView ItemsSource="{Binding AllCompanies}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding Name}" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

After called LoadAllCompanies the count of AllCompanies is 20562104. So my app is very slow now. How to handle this situation?

2
Does this answer your question? Xamarin Forms - Binding Listview for lazy loadingFreakyAli

2 Answers

0
votes

As @Chetan said using API pagination is the best approach . That being said if for some reason you can't change the pagination ( let's say its a third party API) you can still improve your performance by optimizing your caching strategy of the listview as shown in the docs

1
votes

Use Pagination in API side and load data on ListItemAppearing