Does Updating Data need to be on UI Thread?

GreekTreatGreekTreat Member ✭✭
edited October 17 in Xamarin.Forms

Top avoid blocking I want to Use Task.Run(async () => await GenerateData().ConfigureAwait(false)) In my View model to do a long process of pulling data from HTTP and databse and crunch some data. while doing this the UI will have a spinner to state that there is data processing.
in this GenerateData, I update the records inside the ObservableObjects ProductNames like this:
private async Task GenerateData()
{
products = await HTPPCALL();

            ProductNames.Clear();
            foreach(var product in products)
            {
                ProductNames.Add(product.Name);
            }

        }

I've seen this not update the UI, but sometimes in other situations it did.
also on the same note does this need to be done on the main thread?
protected void NotifyPropertyChanged([CallerMemberName]string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

Tagged:

Best Answer

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Xamarin.Forms is written so that bindings are automatically updated on the UI thread, as well as CollectionChanged events on the ListView.ItemSource property. ItemSources on other controls (Picker, Map, etc.) listen to CollectionChanged events but do not marshal handling them to the UI thread.

    I had a discussion with the Xamarin devs about it here and it looks like they may be changing their behavior in regards to CollectionChanged.

Sign In or Register to comment.