refactor(mobile): improve Galeria view and accessory module memory management
- Remove complex deinit and notification observers in GaleriaAccessoryModule - Add weak self references in GaleriaView callback methods to prevent retain cycles Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -54,75 +54,13 @@ public class GaleriaAccessoryModule: Module {
|
||||
hostingVC.view.trailingAnchor.constraint(equalTo: window.trailingAnchor),
|
||||
hostingVC.view.bottomAnchor.constraint(equalTo: window.bottomAnchor),
|
||||
])
|
||||
|
||||
|
||||
// Associate the accessory view with the VC
|
||||
|
||||
GaleriaAccessoryModule.accessoryViews.setObject(hostingVC.view, forKey: galeriaVC)
|
||||
|
||||
var observerRef: NSObjectProtocol?
|
||||
|
||||
// Use the correct notification name and create an observer object
|
||||
observerRef = NotificationCenter.default.addObserver(
|
||||
forName: NSNotification.Name("UIViewControllerDidMoveToParentViewController"),
|
||||
object: galeriaVC,
|
||||
queue: nil
|
||||
) { [weak galeriaVC] _ in
|
||||
guard let vc = galeriaVC else { return }
|
||||
|
||||
if vc.parent == nil {
|
||||
// VC has been removed, clean up related views
|
||||
if let accessoryView = GaleriaAccessoryModule.accessoryViews.object(forKey: vc) {
|
||||
accessoryView.removeFromSuperview()
|
||||
GaleriaAccessoryModule.accessoryViews.removeObject(forKey: vc)
|
||||
}
|
||||
|
||||
// Remove the observer
|
||||
if let observer = observerRef {
|
||||
NotificationCenter.default.removeObserver(observer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add a deinit observer to ensure resources are cleaned up when the VC is released
|
||||
galeriaVC.onDeinit {
|
||||
if let accessoryView = GaleriaAccessoryModule.accessoryViews.object(forKey: galeriaVC) {
|
||||
accessoryView.removeFromSuperview()
|
||||
GaleriaAccessoryModule.accessoryViews.removeObject(forKey: galeriaVC)
|
||||
}
|
||||
|
||||
if let observer = observerRef {
|
||||
NotificationCenter.default.removeObserver(observer)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extension UIViewController to add deinit callback functionality
|
||||
extension UIViewController {
|
||||
private static var deinitCallbackKey = "deinitCallbackKey"
|
||||
|
||||
typealias DeinitCallback = () -> Void
|
||||
|
||||
func onDeinit(_ callback: @escaping DeinitCallback) {
|
||||
let token = DeinitObserver(callback: callback)
|
||||
objc_setAssociatedObject(
|
||||
self, &UIViewController.deinitCallbackKey, token, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
|
||||
}
|
||||
}
|
||||
|
||||
// Auxiliary class for observing deinit
|
||||
private class DeinitObserver {
|
||||
private let callback: () -> Void
|
||||
|
||||
init(callback: @escaping () -> Void) {
|
||||
self.callback = callback
|
||||
}
|
||||
|
||||
deinit {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ class GaleriaView: ExpoView {
|
||||
super.init(appContext: appContext)
|
||||
}
|
||||
|
||||
|
||||
func getChildImageView() -> UIImageView? {
|
||||
var reactSubviews: [UIView]? = nil
|
||||
if RCTIsNewArchEnabled() {
|
||||
@@ -119,18 +120,18 @@ class GaleriaView: ExpoView {
|
||||
}
|
||||
|
||||
options.append(
|
||||
ImageViewerOption.onPreview { index in
|
||||
self.onPreview(["index": index])
|
||||
ImageViewerOption.onPreview { [weak self] index in
|
||||
self?.onPreview(["index": index])
|
||||
})
|
||||
|
||||
options.append(
|
||||
ImageViewerOption.onClosePreview {
|
||||
self.onClosePreview()
|
||||
ImageViewerOption.onClosePreview { [weak self] in
|
||||
self?.onClosePreview()
|
||||
})
|
||||
|
||||
options.append(
|
||||
ImageViewerOption.onIndexChange { index in
|
||||
self.onIndexChange(["index": index])
|
||||
ImageViewerOption.onIndexChange { [weak self] index in
|
||||
self?.onIndexChange(["index": index])
|
||||
})
|
||||
return options
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user