> ## Content Index
> Fetch the complete content index at: https://codetraveler.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# Making ObservableCollection Thread-Safe in Xamarin.Forms
- URL: https://codetraveler.io/2019/09/11/using-observablecollection-in-a-multi-threaded-xamarin-forms-application/
- Published: 2019-09-11T18:50:00.000Z
- Updated: 2019-09-11T18:54:20.000Z
- Author: Brandon Minnick
- Tags: Xamarin.Forms, Xamarin, thread, thread-safe, thread safe, thread safety, thread-safety, ObservableCollection, ListView, ItemSource

`ObservableCollection` is the [recommended collection](https://docs.microsoft.com/xamarin/xamarin-forms/user-interface/listview/data-and-databinding?WT.mc%5Fid=threadsafeobservablecollection-codetraveler-bramin&ref=codetraveler.io) to use for ListViews, but it isn't thread safe. Let's explore how we can fix this and use it in our multi-threaded apps!

The Xamarin.Forms team [recommends using ObservableCollection](https://docs.microsoft.com/xamarin/xamarin-forms/user-interface/listview/data-and-databinding?WT.mc%5Fid=threadsafeobservablecollection-codetraveler-bramin&ref=codetraveler.io) for `ListView.ItemSource`, but when we then try to update the collection from different threads, we'll get this error because `ObservableCollection` isn't thread safe:

```
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index

ListProxy.get_Item (System.Int32 index) D:\a\1\s\Xamarin.Forms.Core\ListProxy.cs:129
IList.get_Item (System.Int32 index)
```

Luckily, the fix is pretty easy!

## Solution

There is a library included in Xamarin.Forms that we can use to ensure the `ObservableCollection` is only updated by one thread at a time: `` `[BindingBase.EnableCollectionSynchronization](https://docs.microsoft.com/dotnet/api/xamarin.forms.bindingbase.enablecollectionsynchronization?view=xamarin-forms&WT.mc%5Fid=threadsafeobservablecollection-codetraveler-bramin&ref=codetraveler.io)` ``.

We just need to call this method in our constructor after initializing our `ObservableCollection` and our collection becomes thread safe:

```csharp
class MyViewModel
{
    public MyViewModel()
    {
        MyCollection = new ObservableCollection<MyModel>();
        Xamarin.Forms.BindingBase.EnableCollectionSynchronization(MyCollection, null, ObservableCollectionCallback);
    }

    public ObservableCollection<MyModel> MyCollection { get; }

    void ObservableCollectionCallback(IEnumerable collection, object context, Action accessMethod, bool writeAccess)
    {
        // `lock` ensures that only one thread access the collection at a time
        lock (collection)
        {
            accessMethod?.Invoke();
        }
    }
}
```