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
}
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
}