美文网首页
iOS ReactNative RCTView在退出ReactN

iOS ReactNative RCTView在退出ReactN

作者: OrrHsiao | 来源:发表于2023-07-10 19:59 被阅读0次

最近将iOS依赖的ReactNative升级到了0.71.4,然后测试上报了一个Bug:iOS桥接给ReactNative的RCTView在退出ReactNative时无法释放。
初步通过断点排查,RCTViewManager在退出ReactNative时已经释放,但是RCTView无法释放。由于工程代码太多,又重新写了一个demo,引用简单些,容易排查,最终发现是在集成ReactNative-navigation库后,此问题就会出现,于是就在github上给该库提了个issue。
一个月后。。。依然无人解决
继续排查,打印RCTView引用计数为4,通过XCode Memory Graph排查demo工程RCTView的引用关系,发现RCTView被RNSScreen持有导致无法释放,然后在github上搜索RNSScreen的issue,发现相似问题,有人给出了解决方案:
//https://github.com/software-mansion/react-native-screens/issues/1754
在ReactNativeViewController的dealloc方法内增加以下代码,在销毁ViewController时销毁RNSScreenView以去除该View对RCTView的强引用

    RCTExecuteOnMainQueue(^{
      NSDictionary *viewRegistry = [self->_bridge.uiManager valueForKey:@"viewRegistry"];
      NSArray *screenViews = [[viewRegistry allValues] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
          return [evaluatedObject isKindOfClass:NSClassFromString(@"RNSScreenView")];
      }]];
      for (id<RCTInvalidating> screenView in screenViews) {
            [screenView invalidate];
      }
    });

相关文章

网友评论

      本文标题:iOS ReactNative RCTView在退出ReactN

      本文链接:https://www.haomeiwen.com/subject/ymtyudtx.html