From b560bcd8dc3522b3ccf1d44662b5c332849f0aa7 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 19 Sep 2022 22:35:27 -0400 Subject: [PATCH] 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 --- ...ffableTimelineLikeTableViewController.swift | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Tusker/Screens/Utilities/DiffableTimelineLikeTableViewController.swift b/Tusker/Screens/Utilities/DiffableTimelineLikeTableViewController.swift index 4bff8236..36706b8c 100644 --- a/Tusker/Screens/Utilities/DiffableTimelineLikeTableViewController.swift +++ b/Tusker/Screens/Utilities/DiffableTimelineLikeTableViewController.swift @@ -25,6 +25,7 @@ class DiffableTimelineLikeTableViewController! @@ -113,13 +114,24 @@ class DiffableTimelineLikeTableViewController 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 }