RN中OC给JS发消息崩溃报错:bridge is not set. This is probably because you've explicitly synthesized the bridge in %@, even though it's inherited from RCTEventEmitter.

该崩溃报错是由于写法不规范导致的

一、官方文档和不合理想法

1.官方文档

//  CalendarManager.h
#import 

@interface CalendarManager : RCTEventEmitter 

@end
//  CalendarManager.m
#import "CalendarManager.h"

@implementation CalendarManager

RCT_EXPORT_MODULE()

- (NSArray *)supportedEvents {
    return @[@"EventReminder"];
}

- (void)calendarEventReminderReceived:(NSNotification *)notification {
    NSString *eventName = notification.userInfo[@"name"];
    [self sendEventWithName:@"EventReminder" body:@{@"name": eventName}];
}

@end

2.不合理想法

由于该文档只是点明了calendarEventReminderReceived方法传的参数是NSNotification类型,我并没有在意这个类型,于是我的做法是在.h中暴露该方法- (void)calendarEventReminderReceived:(NSNotification *)notification;,等我想给JS发消息的时候,直接调用这个方法即可。

二、分析错误

1.初步断定是OC代码的问题

崩溃的意思是没有建立桥接,由于本人从来没有接触过RN,也没写过JS代码,一开始不知道到底是OC的代码有问题,还是JS的代码有问题,通过阅读RCTEventEmitter类的代码发现,可以在CalendarManager.m中重写- (void)startObserving方法和- (void)stopObserving方法来判断JS代码是否写正确。如果JS代码执行addListener添加监听的时候,OC执行了startObserving方法,则证明JS代码没问题。

2.打印桥接属性,判断崩溃原因

阅读RCTEventEmitter类的代码可以发现,该类有一个bridge属性,于是我在startObservingcalendarEventReminderReceived方法里分别打印self.bridge,发现startObserving中有bridge,而calendarEventReminderReceived中为空,什么都没做bridge不可能凭空消失,然后我又分别打印了self,果然,startObservingcalendarEventReminderReceived中打印的self是不一样的。


由此可以知道:当JS代码执行addListener添加监听的时候,就会通过alloc init创建一个CalendarManager对象并建立桥接,我们自己创建的对象是没有桥接的,所以就崩溃了。

三、解决错误

既然找到错误原因了,那就好说了,首先想到的解决办法应该是单例,在CalendarManager.m中添加如下代码即可避免崩溃。

static id _instace;
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instace = [super allocWithZone:zone];
    });
    return _instace;
}

四、优化(合理思路和规范写法)

当我写完单例的时候,我突然想到了官方文档中calendarEventReminderReceived方法传的参数类型是NSNotification。我用OC给JS发送消息的时候,也是用的NSNotificationCenter,当收到通知的时候,我创建一个CalendarManager对象来调calendarEventReminderReceived方法。


这个时候,我突然想扇自己一巴掌:为什么要做这脱裤子放屁的事。直接在CalendarManagerinit方法里面添加通知的监听不就完了吗。。。而且这样不用在别的地方创建CalendarManager对象了,也不用在CalendarManager.m中重写allocWithZone方法来创建单例了

- (instancetype)init {
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(calendarEventReminderReceived:) name:@"customNotificationName" object:nil];
    }
    return self;
}

附:

一、二、崩溃报错代码

//  CalendarManager.h
#import 

@interface CalendarManager : RCTEventEmitter 

- (void)calendarEventReminderReceived:(NSNotification *)notification;

@end
//  CalendarManager.m
#import "CalendarManager.h"

@implementation CalendarManager

RCT_EXPORT_MODULE()

- (NSArray *)supportedEvents {
    return @[@"EventReminder"];
}

- (void)calendarEventReminderReceived:(NSNotification *)notification {
    NSString *eventName = notification.userInfo[@"name"];
    [self sendEventWithName:@"EventReminder" body:@{@"name": eventName}];
}

@end
//  AppDelegate.m
#import "AppDelegate.h"
#import "CalendarManager.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(calendarReceived:) name:@"customNotificationName" object:nil];
    return YES;
}

- (void)calendarReceived:(NSNotification *)notification {
    [[CalendarManager new] calendarEventReminderReceived:notification];
}

三、解决崩溃代码

//  CalendarManager.h
#import 

@interface CalendarManager : RCTEventEmitter 

- (void)calendarEventReminderReceived:(NSNotification *)notification;

@end
//  CalendarManager.m
#import "CalendarManager.h"

@implementation CalendarManager

RCT_EXPORT_MODULE()

static id _instace;

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instace = [super allocWithZone:zone];
    });
    return _instace;
}

- (NSArray *)supportedEvents {
    return @[@"EventReminder"];
}

- (void)calendarEventReminderReceived:(NSNotification *)notification {
    NSString *eventName = notification.userInfo[@"name"];
    [self sendEventWithName:@"EventReminder" body:@{@"name": eventName}];
}

@end
//  AppDelegate.m
#import "AppDelegate.h"
#import "CalendarManager.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(calendarReceived:) name:@"customNotificationName" object:nil];
    return YES;
}

- (void)calendarReceived:(NSNotification *)notification {
    [[CalendarManager new] calendarEventReminderReceived:notification];
}

四、优化之后代码

//  CalendarManager.h
#import 

@interface CalendarManager : RCTEventEmitter 

@end
//  CalendarManager.m
#import "CalendarManager.h"

@implementation CalendarManager

RCT_EXPORT_MODULE()

- (instancetype)init {
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(calendarEventReminderReceived:) name:@"customNotificationName" object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"customNotificationName" object:nil];
}

- (NSArray *)supportedEvents {
    return @[@"EventReminder"];
}

- (void)calendarEventReminderReceived:(NSNotification *)notification {
    NSString *eventName = notification.userInfo[@"name"];
    [self sendEventWithName:@"EventReminder" body:@{@"name": eventName}];
}

@end

JS代码

import {NativeModules, NativeEventEmitter} from 'react-native';

const CalendarManager = new NativeEventEmitter(NativeModules.CalendarManager);
CalendarManager.addListener('EventReminder',(str) => {
    alert(str);
});

你可能感兴趣的:(RN中OC给JS发消息崩溃报错:bridge is not set. This is probably because you've explicitly synthesized the bridge in %@, even though it's inherited from RCTEventEmitter.)