I created the following class (compressed version), heres a link to the full file https://github.com/cotyembry/CastRemoteNative/blob/7e74dbc56f037cc61241f6ece24a94d8c52abb32/root/ios/CastRemoteNative/NativeMethods.swift
@objc(NativeMethods) class NativeMethods: RCTEventEmitter { @objc(sendEventToJSFromJS) func sendEventToJSFromJS { self.emitEvent(eventName: "test", body: "bodyTestString") } func emitEvent(eventName: String: body: Any) { self.sendEvent(withName: eventName, body: body) } }
This works fine and fires my callback listener, which is in my javascript code, when I call the emitEvent method as follows: its modified fragment from https://github.com/cotyembry/CastRemoteNative/blob/7e74dbc56f037cc61241f6ece24a94d8c52j32/32 Components / ChromecastDevicesModal.js
Javascript side
import { NativeModules, NativeEventEmitter } from 'react-native'
I just have a sendEventToJSFromJS method that sendEventToJSFromJS called when a button is clicked in javascript
This works again, and the console.log('in test event listener callback', body) code works and works on the javascript side
My question is where this does NOT work:
If, after defining the class, I had to do the following in a fast file, this will not work:
var nativeMethodsInstance = nativeMethods() nativeMethodsInstance.sendEventToJSFromSwift()
Why? Because the following error occurs:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'bridge is not set. This is probably because you've explicitly synthesized the bridge in NativeMethods, even though it inherited from RCTEventEmitter.'
So, when creating instance NativeMethods, unlike no ... what is the difference?
For more information:
Objective-C gets the same bridge that doesn't pose the problem when I write the same code snippets in .h and .m files, and not in .swift files.
I found where the error message is printed in native code, but it just has a variable
_bridge
and checks if it is nil
Files of this error come from:
RCTEventEmitter.h RCTEventEmitter.c
here is the full fragment of the RCTEventEmitter.c file
- (void)sendEventWithName:(NSString *)eventName body:(id)body { RCTAssert(_bridge != nil, @"bridge is not set. This is probably because you've " "explicitly synthesized the bridge in %@, even though it inherited " "from RCTEventEmitter.", [self class]); if (RCT_DEBUG && ![[self supportedEvents] containsObject:eventName]) { RCTLogError(@"`%@` is not a supported event type for %@. Supported events are: `%@`", eventName, [self class], [[self supportedEvents] componentsJoinedByString:@"`, `"]); } if (_listenerCount > 0) { [_bridge enqueueJSCall:@"RCTDeviceEventEmitter" method:@"emit" args:body ? @[eventName, body] : @[eventName] completion:NULL]; } else { RCTLogWarn(@"Sending `%@` with no listeners registered.", eventName); } }
Where is this _bridge value set and how is it set, so I can know, in cases where it does not work, how to set it
I found the following in RCTEventEmitter.h
@property (nonatomic, weak) RCTBridge *bridge;
The above error mentions that the bridge is inherited in RCTEventEmitter, so maybe the problem is with the weak part in the bridge property?
Or do I need to change my strategy in the way I do all this?
I know that this probably should be due to me, not fully understanding
@synthesize bridge = _bridge;
part of the code and all languages that mix do not help much lol ...
It is really difficult, so any help would be greatly appreciated! Thanks so much for your time.
here is a link to the full project when the project history code represented the code from my question above (since I have made changes to the project since):
https://github.com/cotyembry/CastRemoteNative/tree/7e74dbc56f037cc61241f6ece24a94d8c52abb32