title: Xamarin.Forms (IOS) build list view items dynamically (ObservableCollection and Task.Factory)
body: <p>Directly to the point.
  My list view looks like the following xaml (Not 100% the same but should be detailed enough)</p>
  
  <pre><code>&lt;ListView ItemsSource="{Binding Result}"
                            SelectedItem="{Binding Selected}"
                            HasUnevenRows="True"&gt;My content...&lt;/ListView&gt;
  </code></pre>
  
  <p>This is bound to my view model which looks like this</p>
  
  <pre><code>private CancellationTokenSource cts = null
  private CancellationToken ct = CancellationToken.None
  private List&lt;Task&gt; tasks = new List&lt;Task&gt;()
  public ObservableCollection&lt;T&gt; Source = new ObservableCollection&lt;T&gt;()
  public ObservableCollection&lt;T&gt; Result = new ObservableCollection&lt;T&gt;()
  public string Query
  </code></pre>
  
  <p>It also have the following method. This method is where my problem is. The method should behave like this:</p>
  
  <ul>
  <li>Get all elements from source (doesn't matter from where)</li>
  <li>Start a task which will run in the background (to build the list step by step, to reduce the number of at the same generated listview items, performance)
  
  <ul>
  <li>So it will add the elements to the result step by step (In my case 100 elements max at the same time)</li>
  </ul></li>
  </ul>
  
  <p>And here is the code
  Note: Source has been successfully loaded and the elements are in there</p>
  
  <pre><code>            cts.Cancel();
              cts.Dispose();
              Task.WaitAll(tasks.ToArray());
              tasks.Clear();
              cts = new CancellationTokenSource();
              ct = cts.Token;
              Result.Clear();
              // Create background task
              tasks.Add(Task.Factory.StartNew(() =&gt;
              {
                  var validItems = Source.Where(c =&gt; ((ISearchQueryViewModel)c).SearchData.RegexContains(Query)).OrderByDescending(c =&gt; ((ISearchQueryViewModel)c).Aktive).ToList();
                  if (ct.IsCancellationRequested)
                      return;
                  var numberOfValidItemsToAdd = validItems.Count;
                  var currentIndex = 0;
                  while (numberOfValidItemsToAdd &gt; 0)
                  {
                      if (ct.IsCancellationRequested)
                          return;
                      var numberOfIndexes = 100;
                      if (numberOfValidItemsToAdd &lt; 100)
                          numberOfIndexes = numberOfValidItemsToAdd;
                      for (int i = 0; i &lt; numberOfIndexes; i++)
                      {
                          currentIndex++;
                          if (ct.IsCancellationRequested)
                              return;
                          Result.Add(validItems[i]);
                      }
                      numberOfValidItemsToAdd -= numberOfIndexes;
                      for (int i = 0; i &lt; 100; i++)
                      {
                          if (ct.IsCancellationRequested)
                              return;
                          Thread.Sleep(50);
                      }
                  }
              }, ct));
  </code></pre>
  
  <p>This result the error</p>
  
  <blockquote>
    <p>Unhandled Exception:</p>
    
    <p>Foundation.MonoTouchException: Objective-C exception thrown.  Name:
    NSInternalInconsistencyException Reason: Invalid update: invalid
    number of rows in section 0.  The number of rows contained in an
    existing section after the update (1) must be equal to the number of
    rows contained in that section before the update (1), plus or minus
    the number of rows inserted or deleted from that section (1 inserted,
    0 deleted) and plus or minus the number of rows moved into or out of
    that section (0 moved in, 0 moved out). Native stack trace:   0<br>
    CoreFoundation                      0x00000001104da12b
    __exceptionPreprocess + 171   1   libobjc.A.dylib                     0x000000011dda6f41 objc_exception_throw + 48    2   CoreFoundation<br>
    0x00000001104df2f2 +[NSException raise:format:arguments:] + 98    3<br>
    Foundation                          0x0000000111109d69
    -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193     4<br>
    UIKit                               0x0000000114652209 -[UITableView
    _endCellAnimationsWithContext:] + 19416   5   UIKit                               0x000000011466d075 -[UITableView endUpdates] + 75   6<br>
    MobileClient.iOS 0x000000010fdfd9f9
    xamarin_dyn_objc_msgSend + 217    7   ???<br>
    0x000000013f12d452 0x0 + 5353165906</p>
  </blockquote>