react-native-electrode-bridge icon indicating copy to clipboard operation
react-native-electrode-bridge copied to clipboard

Problem sending a request from Obj-C

Open jduthon opened this issue 7 years ago • 0 comments

I have a problem trying to send a request and I get the following error message:

Bridge is not set. This is probably because you've explicitly synthesized the bridge in ElectrodeBridgeTransceiver, even though it's inherited from RCTEventEmitter.

I am trying to use the ElectrodeRequestProcessor from a Swift file, imported in my AppDelegate.m inside of the ErnRunner.xcodeproj of my electrode-native project. Somehow registering to an event coming from JS works well, but then when I am trying to send an event I run into the above mentioned error.

The relevant Swift code:

    @objc public static func sendRequest(payload: String) {
        let requestProcessor = ElectrodeRequestProcessor<String, String, String>(
            requestName: "communication.native",
            requestPayload: payload,
            respClass: String.self,
            responseItemType: String.self,
            responseCompletionHandler: { responseMessage, failureMessage in
                if let failure = failureMessage {
                    print("Request to JS failed \(failure.message)")
                } else {
                    if let responseMessageString = responseMessage as? String {
                        print("Request response from JS \(responseMessageString)")
                    }
                }
        }
        )
        requestProcessor.execute()
    }

The relevant obj-c code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Start the container
    ElectrodeContainerConfig *containerConfig = [[ElectrodeContainerConfig alloc] init];
    containerConfig.debugEnabled = RnDevSupportEnabled;
    [ElectrodeReactNative startWithConfigurations:containerConfig];
    [RequestHandler startListener];
    [RequestHandler sendRequestWithPayload:@"from native"];
    return YES;
}

Somehow in the real project (importing the miniapp) the same code works well, though I can see 2 main differences:

  • The code is Swift
  • The call to the request is made later

To try to deny/confirm the latest, I added some delay (30s) before trying to send the request, but I get the same error.

If you have any ideas on how to troubleshoot and fix I would be glad, and hope I did not write too much non-sense, I am really new to iOS development.

jduthon avatar Dec 28 '18 10:12 jduthon