react-native 0.51.0 self.bridge 为 null



  • //报错信息
    ios 控制台提示 bridge is not set. This is probably because you've explicitly synthesized the bridge in Native, even though it's inherited from RCTEventEmitter. 然后卡死

    //代码如下
    //.h
    #import <React/RCTBridgeModule.h>
    #import <React/RCTEventEmitter.h>
    
    @interface Native : RCTEventEmitter <RCTBridgeModule>
    
    - (void) emitEventInternal;
    
    @end
    
    //.m
    #import "Native.h"
    #import "AppDelegate.h"
    #import <React/RCTConvert.h>
    
    @implementation Native
    
    RCT_EXPORT_MODULE();
    
    - (NSArray<NSString *> *)supportedEvents
    {
      NSLog(@"----------->supportedEvents--->%@  self-->%@",self.bridge, self);
      return @[@"MessageReminder"];
    }
    
    
    + (id)allocWithZone:(NSZone *)zone {
      static Native *sharedInstance = nil;
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
        sharedInstance = [super allocWithZone:zone];
      });
      return sharedInstance;
    }
    
    
    RCT_EXPORT_METHOD(addEvent)
    {
      [self emitEventInternal];
    }
    
    
    - (void) emitEventInternal {
      NSLog(@"OCsendToReactNative------>%@  self--->%@",self.bridge, self);
      [self sendEventWithName:@"MessageReminder" body:@{
                  @"title" : @"1111",
      }];
      return;
    }
    
    @end
    
    //AppDelegate.m
      Native *native = [[Native alloc] init];
      [native emitEventInternal];
    


  • 你不应该自己初始化这个模块(应该由rn去初始化,这样才会赋予它bridge)

    即便你自己初始化并使用单例模式,也不应该在rn赋予它bridge之前调用



  • 您的意思是让rn来调用这个模块的一个方法来实例化吗



  • 非常感谢确实是我调用太快了 让rn来调用者模块初始化解决了谢谢