Reputation: 477
I have a program that has two tabs, each run with its own view controller. The second tab is a basic screen that displays a button, while the first tab is a UICollectionView
that displays a few UICollectionViewCell
s that have an image and a label.
I seem to have a very large memory leak when switching from the UICollectionView
tab to the other tab. I've determined that both viewcontrollers are properly hitting deinit
, and my UICollectionViewCell
s are also deinitilizing, so I think the issue lies in how I am pulling in my data. It seems to be that the firebase call and the image it returns is never deallocated.
Can someone help figure out what I am doing wrong?
The UICollectionViewCell
is defined as follows
class InteractionViewCell: UICollectionViewCell {
@IBOutlet weak var Photo: UIImageView!
var Id:String?
@IBOutlet weak var personLabel: UILabel!
func configure(_ person:Person) {
self.Id = person:Person.id
FirebaseInstance.GetPersonProfilePicture(person.id) { image in
self.Photo.image = image
}
}
}
And GetPersonProfilePicture
is:
class FirebaseInstance {
static private let StorageInst:StorageReference = Storage.storage().reference()
static var StorageRef:StorageReference = StorageInst
static private let ImageMaxSize = Int64(1 * 1024 * 1024)
static func GetPersonProfilePicture(_ userUID:String, completion: @escaping (UIImage) -> Void) {
StorageRef.child("\(userUID)").getData(maxSize: ImageMaxSize) { data, error in
if let error = error {
print(error.localizedDescription)
return
} else {
let downloadedImage = UIImage(data: data!)
completion(downloadedImage!)
}
}
}
EDIT: If I remove the line where I save the profile picture, the memory leak goes away, which is more proof that the memory leak seems to be in how I download the images.
Thank you in advance!
Upvotes: 0
Views: 316
Reputation: 477
Ah! I figured it out. It actually had nothing to do with how I was downloading the images at all.
My issue was that I was retrieving the cells by calling .addSnapshotListener
on a collection
in the Cloud Firestore
. However, I wasn't ever removing the listener, which meant that my UICollectionView
was never getting deinitialized, which was causing the huge memory leak.
By calling mySnapshotListener.remove()
inside viewWillDisapper
of the first controller, I was able to get rid of the memory leak.
Upvotes: 1