From fc8fcb76fd72313ad2413a4d7219622eaa4d47fe Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 5 Dec 2022 17:17:34 -0500 Subject: [PATCH] Fix crash when TimelineViewController tries to apply snapshot while not visible --- Tusker/Screens/Timeline/TimelineViewController.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Tusker/Screens/Timeline/TimelineViewController.swift b/Tusker/Screens/Timeline/TimelineViewController.swift index e7ff454f..cb833424 100644 --- a/Tusker/Screens/Timeline/TimelineViewController.swift +++ b/Tusker/Screens/Timeline/TimelineViewController.swift @@ -522,10 +522,12 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro private func applySnapshot(_ snapshot: NSDiffableDataSourceSnapshot, maintainingScreenPosition offsetFromTop: CGFloat, ofItem itemToMaintain: Item) { // use a snapshot of the collection view to hide the flicker as the content offset changes and then changes back - let snapshotView = collectionView.snapshotView(afterScreenUpdates: false)! - snapshotView.layer.zPosition = 1000 - snapshotView.frame = view.bounds - view.addSubview(snapshotView) + let snapshotView = collectionView.snapshotView(afterScreenUpdates: false) + if let snapshotView { + snapshotView.layer.zPosition = 1000 + snapshotView.frame = view.bounds + view.addSubview(snapshotView) + } dataSource.apply(snapshot, animatingDifferences: false) { if let indexPathOfItemToMaintain = self.dataSource.indexPath(for: itemToMaintain) { @@ -558,7 +560,7 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro self.collectionView.contentOffset.y -= offsetFromTop } - snapshotView.removeFromSuperview() + snapshotView?.removeFromSuperview() } }