Created
October 15, 2019 14:19
-
-
Save uc-compass-bot/1ff8ea03ec9e7d323c44bc7be4227dd8 to your computer and use it in GitHub Desktop.
Catch Retain Cycles In Collection View Subclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class CollectionView: UICollectionView { | |
private var allCells: [WeakBox<UICollectionViewCell>] = [] | |
private func appendIfNotPresent(_ cell: UICollectionViewCell) { | |
if allCells.first(where: { $0.unbox === cell }) == nil { | |
allCells.append(WeakBox(cell)) | |
} | |
} | |
override func dequeueReusableCell(withReuseIdentifier identifier: String, for indexPath: IndexPath) -> UICollectionViewCell { | |
let cell: UICollectionViewCell = super.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) | |
appendIfNotPresent(cell) | |
return cell | |
} | |
deinit { | |
allCells.forEach { MemoryChecker.verifyDealloc(object: $0.unbox) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment