@@ -685,11 +685,7 @@ final class ModelState {
685685
686686 case let . sectionMove( initialSectionIndex, finalSectionIndex) :
687687 sectionIndicesToDelete. append ( initialSectionIndex)
688- var sectionModelToMove = sectionModelsBeforeBatchUpdates [ initialSectionIndex]
689- // Item moves are handled separately, so we need to clear out existing items
690- for itemIndex in ( 0 ..< sectionModelToMove. numberOfItems) . reversed ( ) {
691- sectionModelToMove. deleteItemModel ( atIndex: itemIndex)
692- }
688+ let sectionModelToMove = sectionModelsBeforeBatchUpdates [ initialSectionIndex]
693689 sectionModelInsertIndexPairs. append ( ( sectionModelToMove, finalSectionIndex) )
694690
695691 case let . itemMove( initialItemIndexPath, finalItemIndexPath) :
@@ -1106,7 +1102,18 @@ final class ModelState {
11061102 {
11071103 // Always insert in ascending order
11081104 for (itemModel, insertIndexPath) in ( itemModelInsertIndexPathPairs. sorted { $0. insertIndexPath < $1. insertIndexPath } ) {
1109- currentSectionModels [ insertIndexPath. section] . insert ( itemModel, atIndex: insertIndexPath. item)
1105+ let sectionIndex = insertIndexPath. section
1106+ let itemIndex = insertIndexPath. item
1107+ let section = currentSectionModels [ sectionIndex]
1108+ if itemIndex < section. numberOfItems, itemModel. id == section. idForItemModel ( atIndex: itemIndex) {
1109+ // If the `itemModel` to insert already exists at the destination index, then there's no need to insert it again. This
1110+ // happens if item move updates are generated in addition to section move updates, which appears to be the case when using
1111+ // `UICollectionViewDiffableDataSource`. Other diffing approaches, like Paul Heckel's, do not produce item moves when
1112+ // their containing sections move.
1113+ continue
1114+ } else {
1115+ currentSectionModels [ insertIndexPath. section] . insert ( itemModel, atIndex: itemIndex)
1116+ }
11101117 }
11111118 }
11121119
0 commit comments