Skip to content

Commit e647cda

Browse files
author
Stephane Magne
authored
Merge pull request #14 from scribd/stephane/delta_safety_check
DRY up safety check, move delegate call onto main thread.
2 parents d2fc890 + 0e58040 commit e647cda

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

Sources/LiveCollections/Classes/Internal/ItemDataCalculator.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,7 @@ private extension ItemDataCalculator {
205205
self?._performNextCalculation()
206206
}
207207

208-
let isDeltaAccurate: Bool = (itemProvider.items.count + delta.insertions.count - delta.deletions.count) == updatedItems.count
209-
210-
if isDeltaAccurate == false {
211-
calculationDelegate?.inaccurateDeltaDetected(delta)
212-
}
213-
214-
guard delta.hasChanges, isDeltaAccurate else {
208+
guard delta.hasChanges else {
215209
updateData()
216210
calculationCompletion()
217211
return
@@ -228,8 +222,13 @@ private extension ItemDataCalculator {
228222
targetView.reloadData()
229223
}
230224

225+
let isDeltaInaccurate: Bool = (itemProvider.items.count + delta.insertions.count - delta.deletions.count) != updatedItems.count
226+
if isDeltaInaccurate {
227+
self.calculationDelegate?.inaccurateDeltaDetected(delta)
228+
}
229+
231230
let itemAnimationStlye: AnimationStyle = {
232-
if targetView.frame.isEmpty { return .reloadData }
231+
if targetView.frame.isEmpty || isDeltaInaccurate { return .reloadData }
233232
guard let animationDelegate = animationDelegate else { return .preciseAnimations }
234233
return animationDelegate.preferredItemAnimationStyle(for: delta)
235234
}()

Sources/LiveCollections/Classes/Internal/SectionDataCalculator.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,8 @@ private extension SectionDataCalculator {
277277

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

282-
if isDeltaAccurate == false {
283-
self.calculationDelegate?.inaccurateDeltaDetected(itemDelta)
284-
}
285-
286-
guard (sectionDelta.hasChanges || itemDelta.hasChanges), isDeltaAccurate else {
281+
guard sectionDelta.hasChanges || itemDelta.hasChanges else {
287282
sectionProvider.calculatingSections = nil
288283
calculationCompletion()
289284
return // don't need to update with no changes
@@ -310,8 +305,13 @@ private extension SectionDataCalculator {
310305
return
311306
}
312307

308+
let isDeltaInaccurate: Bool = (currentItemCount + itemDelta.insertions.count - itemDelta.deletions.count) != updatedItemCount
309+
if isDeltaInaccurate {
310+
self.calculationDelegate?.inaccurateDeltaDetected(itemDelta)
311+
}
312+
313313
let itemAnimationStlye: AnimationStyle = {
314-
if view.frame.isEmpty { return .reloadData }
314+
if view.frame.isEmpty || isDeltaInaccurate { return .reloadData }
315315
guard let animationDelegate = animationDelegate else { return .preciseAnimations }
316316
return animationDelegate.preferredItemAnimationStyle(for: itemDelta)
317317
}()

0 commit comments

Comments
 (0)