I have an ObservableCollection
bound to a ListView
. This List is basically a selection of items. If you click one, its state changes. Its added to favourites and is shown in the favourites list(also a ListView
) or it gets removed from favourites.
This is the main function of my app, so there will be a lot of adding-removing going on. Both lists are slow, buggy and flicker when updated.
How can I go about making it faster/smooth?
I have tried running all add/remove calls on a worker thread. I have tried using a Task
and async
function (which made it worse actually). Is there a "proper" implementation of this scenario?(ListView
ui thread and worker thread keeping it up to date) Is there an article that could teach me the good practise?
Implementation: note: Truck is another class which stores the data. From Data class:
List<Truck> trucks = new List<Truck>();
public void addToFavorites(Truck truck)
{
foreach(Truck t in trucks)
{
if(t == truck)
{
t.setFavorite(true);
}
}
}
public void removeFromFavorites(Truck truck)
{
foreach (Truck t in trucks)
{
if (t == truck)
{
t.setFavorite(true);
}
}
}
public List<Truck> getTrucks()
{
return trucks;
}
public List<Truck> getFavoriteTrucks()
{
List<Truck> temp = new List<Truck>();
foreach(Truck t in trucks)
{
if (t.isFavorite())
{
temp.Add(t);
}
}
return temp;
}
From the page that shows all Trucks:
public partial class AllPage : ContentPage
{
public AllPage(csharp.Data data)
{
//init
InitializeComponent();
this.data = data;
//build list
refreshAsync();
ListView list = new ListView()
{
ItemTemplate = new DataTemplate(typeof(csharp.PlateCell)),
ItemsSource = trucks,
IsPullToRefreshEnabled = true,
};
//on select
list.ItemSelected += (sender, e) => {
if (e.SelectedItem == null) return; //row deselected, dont do anything
var selection = e.SelectedItem as csharp.Truck;
if (selection.isFavorite())
{
data.removeFromFavorites(selection);
selection.setFavorite(false);
}
else {
data.addToFavorites(selection);
selection.setFavorite(true);
}
((ListView)sender).SelectedItem = null; // de-select the row
refreshAsync();
};
list.RefreshCommand = new Command(() =>
{
//trucks = data.getFavoriteTrucks();
refreshAsync();
list.IsRefreshing = false;
});
//add the list to the page
root.Children.Add(list);
}//end constructor
csharp.Data data;
ObservableCollection<csharp.Truck> trucks = new ObservableCollection<csharp.Truck>();
private async Task refreshAsync()
{
await Task.Run(() => refreshThread());
}
private void refreshThread()
{
List<csharp.Truck> all = data.getTrucks();
trucks.Clear();
foreach (csharp.Truck t in all)
{
trucks.Add(t);
}
}
}