alexkendall / react-native-bluetooth-cross-platform

Cross-Platform Bluetooth & WiFi React Native Module. Powered by underdark http://underdark.io/.
179 stars 35 forks source link

NSInternalInconsistencyException. This is probably because you've explicitly synthesized the bridge in NetworkManager, even though it's inherited from RCTEventEmitter. #30

Open ahadcove opened 6 years ago

ahadcove commented 6 years ago

I'm running into an error with a simple new react native app. The only thing that i'm doing is broadcasting the wifi and the app crashes using ios with this error. I still haven't tried it with Android yet

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error when sending event: detectedUser with body: {
    connected = 0;
    id = "%$#90495067-28G5-89BA-TC13-0E0095BUIA8F";
    message = "new user";
    name = MYComp;
    type = advertiser;
}. Bridge is not set. This is probably because you've explicitly synthesized the bridge in NetworkManager, even though it's inherited from RCTEventEmitter.'
azanli commented 6 years ago

+1 I got this error last night. It appears to be iOS only. The strange thing was that I wasn't broadcasting at the time and the app was just idle when the error was thrown. I've only gotten it once but it sufficiently crashes the app into a freeze state.

Note: It has only happened on my iPhone 6S Plus on iOS 11. Both my iPads on iOS 9 haven't experienced a similar issue.

ahadcove commented 6 years ago

@Progoogler Really? For me it was instant, I couldn't get anywhere with this because I couldn't even test. It was this bug for Ios and then Android was a completely different major bug. I'll come back to it at a later date, hopefully when I have time to actually contribute

azanli commented 6 years ago

@AhadCove If I would venture a guess, it'd be that nearby devices are being detected right upon starting the app so NetworkCommunicator invokes self.sendEvent(withName: "detectedUser", body: user.getJSUser("new user")) without having first established the bridge. It would be difficult for me to reproduce this issue since it has only occurred once and never before or yet again for me.

I'll try adding this to fileprivate func checkForNewUsers(_ user: User)

if (self.advertiseTimer != nil) {
    self.sendEvent(withName: "detectedUser", body: user.getJSUser("new user"))
}

Will report back if it happens again afterwards or enough time passes that it doesn't.

azanli commented 6 years ago

@AhadCove Can you try adding this line inside your NetworkManager.m file to see if it will stop throwing this error? The error is very sporadic for me and so far it hasn't thrown the error again yet. I would like to see if that's the same for your case.

- (NSArray<NSString *> *)supportedEvents {
  return @[@"lostUser", @"detectedUser", @"messageReceived", @"connectedToUser", @"receivedInvitation"];
}

So the file looks like this:

#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>

@interface RCT_EXTERN_MODULE(NetworkManager, NSObject)

RCT_EXTERN_METHOD(browse:(NSString*)kind)

RCT_EXTERN_METHOD(advertise:(NSString*)kind)

RCT_EXTERN_METHOD(stopAdvertising)

RCT_EXTERN_METHOD(stopBrowsing)

RCT_EXTERN_METHOD(inviteUser:(NSString*)userId)

RCT_EXTERN_METHOD(acceptInvitation:(NSString*)userId)

RCT_EXTERN_METHOD(getConnectedPeers:(RCTResponseSenderBlock)callback)

RCT_EXTERN_METHOD(getNearbyPeers:(RCTResponseSenderBlock)callback)

RCT_EXTERN_METHOD(sendMessage:(NSString*)message userId:(NSString*)userId)

RCT_EXTERN_METHOD(disconnectFromPeer:(NSString*)peerId);

- (NSArray<NSString *> *)supportedEvents {
  return @[@"lostUser", @"detectedUser", @"messageReceived", @"connectedToUser", @"receivedInvitation"];
}

@end
joshbalfour commented 6 years ago

@Friendly-Robot this fixed it for me - thank you!

joshbalfour commented 6 years ago

ok the above didn’t consistently fix the issue for me but I was able to reliably fix it by refactoring to using a singleton approach: https://github.com/joshbalfour/react-native-bluetooth-cross-platform/commit/29a23b6c15d8fc163f32e228a5ed1486ccbc6ba6 I’ll do a PR and fix my breaking change on android (I renamed the native event listener) tomorrow. I’m not a swift programmer so there might be a more elegant solution but this seems to work reliably ☺️