@@ -82,16 +82,26 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
8282 public func apply( _ sections: [ SPDiffableSection ] , animated: Bool ) {
8383 if #available( iOS 14 , * ) {
8484
85- // Remove section if it deleted from content.
86-
8785 var snapshot = self . snapshot ( )
8886
87+ // Delete
88+
8989 let deletedSections = snapshot. sectionIdentifiers. filter ( { ( checkSection) -> Bool in
9090 return !sections. contains ( where: { $0. identifier == checkSection. identifier } )
9191 } )
9292 if !deletedSections. isEmpty {
9393 snapshot. deleteSections ( deletedSections)
94- apply ( snapshot, animated: true )
94+ }
95+
96+ // Add
97+
98+ let addedSections = sections. filter { checkSection in
99+ return !snapshot. sectionIdentifiers. contains ( where: {
100+ $0. identifier == checkSection. identifier
101+ } )
102+ }
103+ if !addedSections. isEmpty {
104+ snapshot. appendSections ( addedSections)
95105 }
96106
97107 // Reoder
@@ -105,11 +115,11 @@ open class SPDiffableCollectionDataSource: UICollectionViewDiffableDataSource<SP
105115 snapshot. moveSection ( section, afterSection: previousSection)
106116 }
107117
108- // Apply Sections Changes
118+ // Apply Changes
109119
110120 apply ( snapshot, animated: true )
111121
112- // Update current sections.
122+ // Update items in Sections
113123
114124 for section in sections {
115125 var sectionSnapshot = SPDiffableSectionSnapshot ( )
0 commit comments