Skip to content

Commit dd06d8b

Browse files
okmyselfdavdroman
andauthored
Fix memory leak in #161 and regression in #194 (#192)
Co-authored-by: David Roman <[email protected]>
1 parent fc40fb1 commit dd06d8b

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

Introspect/UIKitIntrospectionView.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,18 @@ public struct UIKitIntrospectionView<TargetViewType: UIView>: UIViewRepresentabl
6363
return view
6464
}
6565

66+
/// In some cases, UIView does not call `moveToWindowHandler`,
67+
/// so the `moveToWindowHandler` call in updateUIView must be preserved.
6668
public func updateUIView(
6769
_ uiView: IntrospectionUIView,
6870
context: UIViewRepresentableContext<UIKitIntrospectionView>
69-
) {}
71+
) {
72+
uiView.moveToWindowHandler?()
73+
}
74+
75+
/// Avoid memory leaks.
76+
public static func dismantleUIView(_ uiView: IntrospectionUIView, coordinator: ()) {
77+
uiView.moveToWindowHandler = nil
78+
}
7079
}
7180
#endif

Introspect/UIKitIntrospectionViewController.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,18 @@ public struct UIKitIntrospectionViewController<TargetViewControllerType: UIViewC
5252
return viewController
5353
}
5454

55+
/// If you find that the `moveToWindowHandler` is not called in certain situations (which has not been discovered yet),
56+
/// you can add code to call the `moveToWindowHandler` in the function body.
5557
public func updateUIViewController(
56-
_ uiViewController: IntrospectionUIViewController,
58+
_ viewController: IntrospectionUIViewController,
5759
context: UIViewControllerRepresentableContext<UIKitIntrospectionViewController>
58-
) {}
60+
) {
61+
(viewController.view as? IntrospectionUIView)?.moveToWindowHandler?()
62+
}
63+
64+
/// Avoid memory leaks.
65+
public static func dismantleUIViewController(_ viewController: IntrospectionUIViewController, coordinator: ()) {
66+
(viewController.view as? IntrospectionUIView)?.moveToWindowHandler = nil
67+
}
5968
}
6069
#endif

0 commit comments

Comments
 (0)