RN与swift混合开发, RCTViewManager内存释放问题

在reactnative和iOS混合开发过程中, 继承自RCTViewManager的原生组件, 必须实现方法:

override func view() -> UIView {

}

由于对RCTViewManager的docs没有细看, 导致出现如下代码:

lazy var testView: TestView = {
        let view = TestView()
        return view
 }()

override func view() -> UIView {
       return  testView
}

这样会导致该testView无法释放, 无论多少界面使用该testView, 加载的始终是一个view, init方法只走一次, 通过对RCTViewManager文档的了解, 表明该view不该被缓存, 始终加载的是新的view

/**
 * This method instantiates a native view to be managed by the module. Override
 * this to return a custom view instance, which may be preconfigured with default
 * properties, subviews, etc. This method will be called many times, and should
 * return a fresh instance each time. The view module MUST NOT cache the returned
 * view and return the same instance for subsequent calls.
 */
- (UIView *)view;

解决方法:

override func view() -> UIView {
       return  TestView()
}

小细节, 特此小记!!!

你可能感兴趣的:(RN与swift混合开发, RCTViewManager内存释放问题)