Why you should never leave your Closures/Blocks strong?

Because
A great mind once said:
Don’t let anything become strong enough to destroy you.
Just kidding!
It’s just something I came up with myself.
A Bug Fix
I was working on a bug fix. I did fix it but I didn’t know it’ll have a cascading effect on some other feature. What I did was simply update the UI after the data has been fetched from the API on the main thread. It was a Home screen on which we have hidden the navigation bar inside the function that (I was unaware of the navigation bar being hidden was also inside that function) I used to update the UI.
A New Bug caused by the Bug Fix
Two days later QA approached me with a bug where navigation bar was hiding on its own after a few seconds when going to another controller from Home controller. The bug was only occurring if you go to another controller from Home as soon as the application opens. The bug was really strange because in the viewDidLoad of the view controller, setNavigationBarHidden was set to NO and it wasn’t working. Then I tried setting setNavigationBarHidden to NO in places where there were API calls. This didn’t seem to work either. I searched the entire project for setNavigationBarHidden and the only place I found it to be set to YES was inside a controller which wasn’t being used. It was kind of annoying, because I couldn’t seem to actually find from where setNavigationBarHidden is being set to YES.
The Bug Fix
Well of course I was tired and literally like:
So I decided to ask help from a colleague (you guys should also try this. It always helps when you discuss things).
First he also tried the same approaches that I did, then in order to debug the setNavigationBarHidden property we decided to override the setNavigationBarHidden property and see from what places the property is being set (I don’t know why it didn’t come to my mind in the start).
After debugging, it brought me to the UI update function that I called inside the main thread on Home controller two days ago. Since the UI update function was written inside dispatch_get_main_queue() which is a closure. Inevitably, catching the Home controller’s self inside which was causing the navigation bar to hide.
Thus, providing the UI update function inside the dispatch_get_main_queue() with a weak self resolved the issue.
Conclusion
For all the people wondering why I only found one setNavigationBarHidden set to YES after searching the entire project. Reason was the property used to hide the navigation there was navigationBarHidden directly instead of it’s setter. My bad. Forgive me for being too humanly. :D
So yeah, a mere strong closure/block can waste a lot of your time.
Lesson learned!
