Prevent loading indicator from potentially being added multiple times

Not sure how this could happen, but it's caused 1 crash in the wild so w/e
This commit is contained in:
Shadowfacts 2022-09-19 22:35:27 -04:00
parent 85ced7ff5f
commit b560bcd8dc
1 changed files with 15 additions and 3 deletions

View File

@ -25,6 +25,7 @@ class DiffableTimelineLikeTableViewController<Section: DiffableTimelineLikeSecti
private(set) var state = State.unloaded
private var lastLastVisibleRow: IndexPath?
private var currentLoadingIndicatorWorkItem: DispatchWorkItem?
private(set) var dataSource: UITableViewDiffableDataSource<Section, Item>!
@ -113,13 +114,24 @@ class DiffableTimelineLikeTableViewController<Section: DiffableTimelineLikeSecti
}
private func showLoadingIndicatorDelayed() -> DispatchWorkItem {
currentLoadingIndicatorWorkItem?.cancel()
let workItem = DispatchWorkItem { [weak self] in
guard let self = self else { return }
var snapshot = self.dataSource.snapshot()
snapshot.appendSections([.loadingIndicator])
snapshot.appendItems([.loadingIndicator])
self.dataSource.apply(snapshot, animatingDifferences: false)
var changed = false
if !snapshot.sectionIdentifiers.contains(.loadingIndicator) {
snapshot.appendSections([.loadingIndicator])
changed = true
}
if changed || !snapshot.itemIdentifiers(inSection: .loadingIndicator).contains(.loadingIndicator) {
snapshot.appendItems([.loadingIndicator], toSection: .loadingIndicator)
changed = true
}
if changed {
self.dataSource.apply(snapshot, animatingDifferences: false)
}
}
currentLoadingIndicatorWorkItem = workItem
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(250), execute: workItem)
return workItem
}