title: SQL Xamarin Forms Delete Item Duplication
body: <p>I have an application where items are added to a collection view which is swipable to be deleted. I am using SQL to do so. However, when deleting the item, sometimes the items have 'duplicated' until a refresh happens and then they disappear once again. How do I go about fixing this issue and why does it only occur sometimes? (See below for bug).</p>
  <p>Here is the Xaml:</p>
  <pre><code>&lt;RefreshView x:DataType=&quot;local:ItemsViewModel&quot; Command=&quot;{Binding LoadItemsCommand}&quot; IsRefreshing=&quot;{Binding IsBusy, Mode=TwoWay}&quot;&gt;
      &lt;CollectionView x:Name=&quot;ItemsListView&quot;
              ItemsSource=&quot;{Binding Items}&quot;
              SelectionMode=&quot;None&quot;&gt;
          &lt;CollectionView.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                   &lt;SwipeView&gt;
                      &lt;SwipeView.RightItems&gt;
                          &lt;SwipeItems Mode=&quot;Execute&quot;&gt;
                              &lt;SwipeItem Text=&quot;Delete&quot; 
                                         BackgroundColor=&quot;Red&quot;
                                         Command=&quot;{Binding Source={RelativeSource AncestorType={x:Type local:ItemsViewModel}}, Path=DeleteItemCommand}&quot;
                                         CommandParameter=&quot;{Binding Source={RelativeSource Self}, Path=BindingContext}&quot;/&gt;
                          &lt;/SwipeItems&gt;
                      &lt;/SwipeView.RightItems&gt;
                      &lt;StackLayout Padding=&quot;10&quot; x:DataType=&quot;model:Item&quot; BackgroundColor=&quot;#333333&quot;&gt;
                          &lt;Label Text=&quot;{Binding Text}&quot;
                                 LineBreakMode=&quot;NoWrap&quot; 
                                 Style=&quot;{DynamicResource ListItemTextStyle}&quot; 
                                 FontSize=&quot;16&quot;
                                 FontAttributes=&quot;Bold&quot;
                                 TextColor=&quot;White&quot;/&gt;
                          &lt;Label Text=&quot;{Binding Description}&quot; 
                                 LineBreakMode=&quot;NoWrap&quot;
                                 Style=&quot;{DynamicResource ListItemDetailTextStyle}&quot;
                                 FontSize=&quot;13&quot;
                                 TextColor=&quot;White&quot;/&gt;
                          &lt;Label Text=&quot;{Binding DueDate}&quot;
                                 Style=&quot;{DynamicResource ListItemDetailTextStyle}&quot;
                                 FontSize=&quot;13&quot;
                                 TextColor=&quot;White&quot;/&gt;
                          &lt;StackLayout.GestureRecognizers&gt;
                              &lt;TapGestureRecognizer 
                              NumberOfTapsRequired=&quot;1&quot;
                              Command=&quot;{Binding Source={RelativeSource AncestorType={x:Type local:ItemsViewModel}}, Path=ItemTapped}&quot;     
                              CommandParameter=&quot;{Binding .}&quot;&gt;
                              &lt;/TapGestureRecognizer&gt;
                          &lt;/StackLayout.GestureRecognizers&gt;
                     &lt;/StackLayout&gt;
                  &lt;/SwipeView&gt;
              &lt;/DataTemplate&gt;
              &lt;/CollectionView.ItemTemplate&gt;
          &lt;/CollectionView&gt;
  &lt;/RefreshView&gt;
  </code></pre>
  <p>Here is the delete function:</p>
  <pre><code>private async Task OnDeleteItem(Item selectedItem)
  {
      await LocalDatabaseService.DeleteItem(selectedItem);
  }
  </code></pre>
  <p>SQL Delete:</p>
  <pre><code>public async Task DeleteItem(Item item)
  {
      var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), &quot;MyData3.db&quot;);
      var db = new SQLiteAsyncConnection(databasePath);
  
      await db.DeleteAsync(item);
      OnDeletedItem?.Invoke();
  }
  </code></pre>
  <p>OnDeleteItem?.Invoke();</p>
  <pre><code>private void LocalDatabaseService_OnDeletedItem()
  {
      _ = ExecuteLoadItemsCommand();
  }
  </code></pre>
  <p>ExecuteLoadItemsCommand();</p>
  <pre><code>async Task ExecuteLoadItemsCommand()
  {
      IsBusy = true;
  
      try
      {
          Items.Clear();
          var items = await LocalDatabaseService.GetAllItems();
          foreach (var item in items)
          {
              Items.Add(item);
          }
      }
      catch (Exception ex)
      {
          Debug.WriteLine(ex);
      }
      finally
      {
          IsBusy = false;
      }
  }
  </code></pre>
  <p>Here is what happens occasionally:
  <a href="https://i.stack.imgur.com/VnF7e.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VnF7e.gif" alt="enter image description here" /></a></p>
  <p>any help is appreciated, Thanks.</p>