objective-c - React Native:通过事件总线发送事件时应用程序崩溃
问题描述
这是我第一次必须在 Objective C 代码中修复某些东西。我需要使用 react native 将事件从 Objective C 发送到 JS/typescript。我已经尝试过这里的建议:
https://stackoverflow.com/questions/36092903/listening-for-events-in-react-native-ios/39437462
我没有在该问题或其他问题中报告的相同问题。我可以接收事件。问题是应用程序随机崩溃。
在Objective CI中有接口的定义:
@interface MyEventModule : RCTEventEmitter <RCTBridgeModule>
+ (id)singletonInstance;
@end
它的实现:
@implementation MyEventModule
{
bool hasListeners;
}
- (NSArray<NSString *> *) supportedEvents {
return @[@“MY_EVENT"];
}
- (void)sendEventWithNameAndBody:(NSString *)name body:(NSString *)body {
if (hasListeners) {
[self sendEventWithName:name body: body];
}
}
RCT_EXPORT_MODULE(MyEventModule);
+ (id)singletonInstance {
static MyEventModule *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
-(void)startObserving {
hasListeners = YES;
}
-(void)stopObserving {
hasListeners = NO;
}
@end
及其用法:
MyEventModule *eventEmitter = [MyEventModule singletonInstance];
[eventEmitter sendEventWithName:@"MY_EVENT" body:@"TEST"];
在打字稿中,我有:
const { MyEventModule } = NativeModules;
const MyEventEmitter = new NativeEventEmitter(MyEventModule)
MyEventEmitter.addListener("MY_EVENT", (body) => console.info("NativeEventEmitter MY_EVENT", body))
屏幕锁定时发送事件。该应用程序不会一直崩溃,但它经常崩溃并出现以下错误:
libc++abi.dylib:以 std::__1::system_error 类型的未捕获异常终止:互斥锁失败:无效参数以 std::__1::system_error 类型的未捕获异常终止:互斥锁失败:参数无效
解决方案
推荐阅读
- rust - 是否可以遍历元组?
- angular - *ngIf 用于所有子路由
- react-native - react-native 上的 web3:错误:返回错误:gas 资金不足 * 价格 + 价值
- php - 如何检查请求何时发送
- c++ - 如何在派生类上使用初始化列表?
- python - 如何在 Python 中使用用户的输入调用方法
- reactjs - 一个功能组件内部可以有多个功能吗?
- excel - 将多个文件从不同文件夹复制到不同的目标文件夹
- reactjs - react.js 设置默认路由
- r - eval(substitute(x), data, parent.frame()) 中的错误:缺少参数“data”,没有默认值