Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ExampleApp/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

private func makeUITestsCallback() -> DeallocationChecker.Callback {
return { leakState, _ in
return { leakState, _, _ in
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIViewController()
window.makeKeyAndVisible()
Expand Down
26 changes: 22 additions & 4 deletions Sources/DeallocationChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,26 @@ public class DeallocationChecker: NSObject {
case leaked
case notLeaked
}

public enum DisappearanceSource: CustomStringConvertible, CustomDebugStringConvertible {
case parent
case dismissed

public var description: String {
return self.debugDescription
}

public var debugDescription: String {
switch self {
case .parent:
return "parent"
case .dismissed:
return "dismissed"
}
}
}

public typealias Callback = (LeakState, UIViewController.Type) -> ()
public typealias Callback = (LeakState, UIViewController.Type, DisappearanceSource) -> ()

public enum Handler {
/// Shows alert when a leak is detected.
Expand Down Expand Up @@ -66,7 +84,7 @@ public class DeallocationChecker: NSObject {
// and present the wrapping view controller instead.
if viewController.isMovingFromParent || rootParentViewController.isBeingDismissed {
let viewControllerType = type(of: viewController)
let disappearanceSource: String = viewController.isMovingFromParent ? "removed from its parent" : "dismissed"
let disappearanceSource: DisappearanceSource = viewController.isMovingFromParent ? .parent : .dismissed

DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: { [weak viewController] in
let leakState: LeakState = viewController != nil ? .leaked : .notLeaked
Expand All @@ -78,10 +96,10 @@ public class DeallocationChecker: NSObject {
}
case .precondition:
if leakState == .leaked {
preconditionFailure("\(viewControllerType) not deallocated after being \(disappearanceSource)")
preconditionFailure("\(viewControllerType) not deallocated after being \(disappearanceSource == .parent ? "removed from its parent" : "dismissed")")
}
case let .callback(callback):
callback(leakState, viewControllerType)
callback(leakState, viewControllerType, disappearanceSource)
}
})
}
Expand Down