So I had a problem with the app I'm working on about a memory crash.
The app had a websniffer which was downloading images from a website and
displaying them in a CollectionView.
If the images are too big, this would cause a crash since they would overload the
memory allocated for the app and iOS
would kill the app after throwing memory errors.
The problem was, the websniffer had no limit to the amount of images it would
download. To stop this, I added a
stop condition to the websniffer which continuously downloads images. The stop
condition is basically checking the amount of memory the images array uses and if it's
over a certain amount, it'll tell the websniffer to stop sniffing.
Here is the code that explains this:
The arraySize function is below which actually calculates the size of a given array:
To explain further more, the paths variable contains the URLs pointing to the imagres.
The async block imageDownloaded throws the images in to the images array and
continues to fetch them until all of them
are downloaded or the memoryLimit has been reached.
Below is the code for the getImageFromAddress function:
Hope this idea helps someone out there who is struggling with a similar issue.