Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Does Updating Data need to be on UI Thread?

GreekTreatGreekTreat Member ✭✭
edited October 2019 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.