From 45ed97c25579f780db80d430baa2809230d447b7 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Mon, 10 Jul 2023 15:38:02 -0500 Subject: [PATCH] Actually fixes crash loading save states on iOS 17 The underlying issue causing the crash is that we were returning cached *supplementary view* layout attributes by accident from layoutAttributesForItem(at:). Now, we only cache layout attributes for cells. --- .../Collection View/GridCollectionViewLayout.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Delta/Components/Collection View/GridCollectionViewLayout.swift b/Delta/Components/Collection View/GridCollectionViewLayout.swift index 0b34efe..38c563d 100644 --- a/Delta/Components/Collection View/GridCollectionViewLayout.swift +++ b/Delta/Components/Collection View/GridCollectionViewLayout.swift @@ -50,7 +50,7 @@ class GridCollectionViewLayout: UICollectionViewFlowLayout return interitemSpacing } - private var cachedLayoutAttributes = [IndexPath: UICollectionViewLayoutAttributes]() + private var cachedCellLayoutAttributes = [IndexPath: UICollectionViewLayoutAttributes]() override var estimatedItemSize: CGSize { didSet { @@ -73,8 +73,8 @@ class GridCollectionViewLayout: UICollectionViewFlowLayout if let context = context as? UICollectionViewFlowLayoutInvalidationContext, context.invalidateFlowLayoutAttributes || context.invalidateFlowLayoutDelegateMetrics || context.invalidateEverything { - // We must clear layout cache on iOS 17 or later to prevent crashing due to returning outdated layout attributes. - self.cachedLayoutAttributes = [:] + // Clear layout cache to prevent crashing due to returning outdated layout attributes. + self.cachedCellLayoutAttributes = [:] } } @@ -151,10 +151,10 @@ class GridCollectionViewLayout: UICollectionViewFlowLayout } } - for attributes in layoutAttributes + for attributes in layoutAttributes where attributes.representedElementCategory == .cell { // Update cached attributes for layoutAttributesForItem(at:) - self.cachedLayoutAttributes[attributes.indexPath] = attributes + self.cachedCellLayoutAttributes[attributes.indexPath] = attributes } return layoutAttributes @@ -162,7 +162,7 @@ class GridCollectionViewLayout: UICollectionViewFlowLayout override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { - if let cachedAttributes = self.cachedLayoutAttributes[indexPath] + if let cachedAttributes = self.cachedCellLayoutAttributes[indexPath] { return cachedAttributes }