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.
This commit is contained in:
Riley Testut 2023-07-10 15:38:02 -05:00
parent 31578e2e34
commit 45ed97c255

View File

@ -50,7 +50,7 @@ class GridCollectionViewLayout: UICollectionViewFlowLayout
return interitemSpacing return interitemSpacing
} }
private var cachedLayoutAttributes = [IndexPath: UICollectionViewLayoutAttributes]() private var cachedCellLayoutAttributes = [IndexPath: UICollectionViewLayoutAttributes]()
override var estimatedItemSize: CGSize { override var estimatedItemSize: CGSize {
didSet { didSet {
@ -73,8 +73,8 @@ class GridCollectionViewLayout: UICollectionViewFlowLayout
if let context = context as? UICollectionViewFlowLayoutInvalidationContext, if let context = context as? UICollectionViewFlowLayoutInvalidationContext,
context.invalidateFlowLayoutAttributes || context.invalidateFlowLayoutDelegateMetrics || context.invalidateEverything 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. // Clear layout cache to prevent crashing due to returning outdated layout attributes.
self.cachedLayoutAttributes = [:] 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:) // Update cached attributes for layoutAttributesForItem(at:)
self.cachedLayoutAttributes[attributes.indexPath] = attributes self.cachedCellLayoutAttributes[attributes.indexPath] = attributes
} }
return layoutAttributes return layoutAttributes
@ -162,7 +162,7 @@ class GridCollectionViewLayout: UICollectionViewFlowLayout
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
{ {
if let cachedAttributes = self.cachedLayoutAttributes[indexPath] if let cachedAttributes = self.cachedCellLayoutAttributes[indexPath]
{ {
return cachedAttributes return cachedAttributes
} }