Created
January 30, 2019 06:56
-
-
Save HassanSE/5a51ff33a5ee77c9cb1fc69b03130223 to your computer and use it in GitHub Desktop.
Memory leak catcher
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
extension UIViewController { | |
public func dch_checkDeallocation(afterDelay delay: TimeInterval = 5.0) { | |
let rootParentViewController = dch_rootParentViewController | |
// We don’t check `isBeingDismissed` simply on this view controller because it’s common | |
// to wrap a view controller in another view controller (e.g. in UINavigationController) | |
// and present the wrapping view controller instead. | |
if isMovingFromParent || rootParentViewController.isBeingDismissed { | |
let selfType = type(of: self) | |
let disappearanceSource: String = isMovingFromParent ? "removed from its parent" : "dismissed" | |
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: { [weak self] in | |
assert(self == nil, "\(selfType) not deallocated after being \(disappearanceSource)") | |
}) | |
} | |
} | |
private var dch_rootParentViewController: UIViewController { | |
var root = self | |
while let parent = root.parent { | |
root = parent | |
} | |
return root | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment