Skip to content

Commit

Permalink
Merge pull request #13 from scribd/stephane/safety_check
Browse files Browse the repository at this point in the history
Add delta safety check.
  • Loading branch information
Stephane Magne authored Nov 7, 2023
2 parents fb4f611 + 2707630 commit d2fc890
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,14 @@ private extension ItemDataCalculator {
}
self?._performNextCalculation()
}

guard delta.hasChanges else {

let isDeltaAccurate: Bool = (itemProvider.items.count + delta.insertions.count - delta.deletions.count) == updatedItems.count

if isDeltaAccurate == false {
calculationDelegate?.inaccurateDeltaDetected(delta)
}

guard delta.hasChanges, isDeltaAccurate else {
updateData()
calculationCompletion()
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,16 @@ private extension SectionDataCalculator {
}
return
}

guard sectionDelta.hasChanges || itemDelta.hasChanges else {

let currentItemCount: Int = sectionProvider.sections.reduce(0) { $0 + $1.items.count }
let updatedItemCount: Int = updatedSections.reduce(0) { $0 + $1.items.count }
let isDeltaAccurate: Bool = (currentItemCount + itemDelta.insertions.count - itemDelta.deletions.count) == updatedItemCount

if isDeltaAccurate == false {
self.calculationDelegate?.inaccurateDeltaDetected(itemDelta)
}

guard (sectionDelta.hasChanges || itemDelta.hasChanges), isDeltaAccurate else {
sectionProvider.calculatingSections = nil
calculationCompletion()
return // don't need to update with no changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public protocol CollectionDataCalculationNotificationDelegate: AnyObject {

func collectionDataDidBeginCalculating()
func collectionDataDidEndCalculating()
func inaccurateDeltaDetected(_ delta: IndexDelta)
}

// MARK: CollectionDataAnimationDelegate
Expand Down

0 comments on commit d2fc890

Please sign in to comment.