title: Xamarin Forms: Should ObservableCollection always be set/updated in the UI thread?
body: <p>Recently, I had to implement infinite scrolling / lazy loading on my PCL ListView. I will leave out most of the code, but the most important part was:</p>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>   var countries = // get countries
     foreach (var country in countries)
     {
          // Countries is an ObservableCollection&lt;Country&gt; 
          Countries.Add(country);
     }
  </code></pre>
  
  <p>This seemed to work fine on Android, but on iOS, I kept getting out of range exceptions especially when I scrolled the list fast. The fix for me was to run this code in the main UI thread.</p>
  
  <pre><code>// wrap the code with this
  Device.BeginInvokeOnMainThread(async () =&gt; {});
  </code></pre>
  
  <p>My question now is should all view model service calls that update or set an observable collection be always executed in the UI thread?</p>
  
  <p>I have several commands that <strong>set</strong> <code>Countries</code>. They seem to work fine without the UI thread block. I only had issues with <strong>adding</strong> items as indicated above.</p>
  
  <p>Should ObservableCollection always be set and updated in the UI thread?</p>