Created
November 2, 2016 23:25
-
-
Save michalciurus/2997fb92a0067adeb355fa16648fc8e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Retain cycles are very easy to make when you have complex closures | |
//For example here `childRouter` is being captured as a strong in the closure | |
//Causing a leak for ALL routers in my app | |
//The funny thing was that I was looking at this piece of code for 15 minutes and couldn't find anything wrong | |
func addChildRouter(childRouter : Router) { | |
childRouters.append(childRouter) | |
if let removeObservable = childRouter.removeFromParentRouter { | |
removeObservable.subscribeNext({ [weak self] in | |
if let `self` = self { | |
let index = self.childRouters.indexOf({ (router) -> Bool in | |
return router === childRouter //STRONG REFERENCE TO CHILD ROUTER, USE [weak childRouter] | |
}) | |
if let index = index { | |
self.childRouters.removeAtIndex(index) | |
} | |
} | |
}) | |
.addDisposableTo(disposeBag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment