Skip to content

Instantly share code, notes, and snippets.

@uc-compass-bot
Created October 15, 2019 14:19
Show Gist options
  • Save uc-compass-bot/1ff8ea03ec9e7d323c44bc7be4227dd8 to your computer and use it in GitHub Desktop.
Save uc-compass-bot/1ff8ea03ec9e7d323c44bc7be4227dd8 to your computer and use it in GitHub Desktop.
Catch Retain Cycles In Collection View Subclass
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