How to trigger the view controller memory warning on tests in Swift?

Subscribe to my newsletter and never miss my upcoming articles

Hello brothers and sisters, Leo here.

Today we'll explore some hack to trigger the memory on view controller. Let's go!


You have to test the code inside the didReceiveMemoryWarning() method from a view controller in your test suite.

This definitely is a tricky problem. First because if you try yourself to cause a memory warning, you'll probably ending in a situation that your unit test aren't reliable as they should be because you don't have control when the system will call the memory warning method, so the runtime of the tests will be unstable.

Fortunately you have the option to trigger manually calling this piece of code inside your tests case:


And that's it!

I hope you enjoy this and thanks for the reading!

credit: credit

No Comments Yet