I have a viewController that has a collectionView outlet. In the cellForRowAt method I call this code:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as? MediaPhotoCell else {fatalError("Could not initialize cell")}
cell.configureForMedia(obj, completion: { [weak self](image:UIImage?) in
if let loadedImage = image {
let photo = self?.loadedPhotos[indexPath.row]
photo?.image = loadedImage
self?.loadedPhotos[indexPath.row] = photo
self?.photosViewController?.updateImage(for: photo)
}
})
return cell
I've been spending time on learning how to resolve/prevent memory leaks through retain cycles. I added a capture list [weak self] before capturing the value in the closure (image: UIIimage?). My logic is that since viewController owns the collectionview, and that collectionview owns the cell, which has a closure method in it's custom implementation (custom cell), and closure references self, i'm creating a retain cycle if i do not declare self as weak.
Is this necessary? Or is this not necessary if the outlet (the collectionView) is declared as weak?
configureForMedia, but usuallycompletionblocks are not retained indefinitely and are intended to be released after the method's action is taken. In this case, it may be more beneficial to keep a strong reference toself. There is no retain cycle here. - Ian MacDonald