Skip to content

Instantly share code, notes, and snippets.

@michalciurus
Created November 2, 2016 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalciurus/2997fb92a0067adeb355fa16648fc8e1 to your computer and use it in GitHub Desktop.
Save michalciurus/2997fb92a0067adeb355fa16648fc8e1 to your computer and use it in GitHub Desktop.
//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