问题描述
这是我第一次必须修复 Objective C 代码中的某些内容。 我需要使用 react native 将事件从 Objective C 发送到 JS/typescript。 我已经尝试过这里的建议:
https://stackoverflow.com/questions/36092903/listening-for-events-in-react-native-ios/39437462
我没有在该问题或其他问题中报告的相同问题。我可以接收事件。问题是应用程序随机崩溃。
在 Objective C 中,我有接口的定义:
@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 类型的未捕获异常终止:互斥锁失败:参数无效
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)