Skip to content

Commit bbe9dab

Browse files
committed
Only initialize the native client once
1 parent b06ec1a commit bbe9dab

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

android/src/main/java/io/sentry/RNSentryModule.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class RNSentryModule extends ReactContextBaseJavaModule {
5656
final static Logger logger = Logger.getLogger("react-native-sentry");
5757
private static WritableNativeMap extra;
5858
private static ReadableMap tags;
59+
private static SentryClient sentryClient;
5960

6061
public RNSentryModule(ReactApplicationContext reactContext, ReactApplication reactApplication) {
6162
super(reactContext);
@@ -83,7 +84,11 @@ public Map<String, Object> getConstants() {
8384

8485
@ReactMethod
8586
public void startWithDsnString(String dsnString) {
86-
SentryClient sentryClient = Sentry.init(dsnString, new AndroidSentryClientFactory(this.getReactApplicationContext()));
87+
if (sentryClient != null) {
88+
logger.info(String.format("Already started, use existing client '%s'", dsnString));
89+
return;
90+
}
91+
sentryClient = Sentry.init(dsnString, new AndroidSentryClientFactory(this.getReactApplicationContext()));
8792
androidHelper = new AndroidEventBuilderHelper(this.getReactApplicationContext());
8893
sentryClient.addEventSendCallback(new EventSendCallback() {
8994
@Override

ios/RNSentry.m

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,27 +181,30 @@ - (void)setReleaseVersionDist:(SentryEvent *)event {
181181

182182
RCT_EXPORT_METHOD(startWithDsnString:(NSString * _Nonnull)dsnString)
183183
{
184-
NSError *error = nil;
185-
SentryClient *client = [[SentryClient alloc] initWithDsn:dsnString didFailWithError:&error];
186-
[SentryClient setSharedClient:client];
187-
[SentryClient.sharedClient startCrashHandlerWithError:&error];
188-
if (error) {
189-
[NSException raise:@"SentryReactNative" format:@"%@", error.localizedDescription];
190-
}
191-
SentryClient.sharedClient.shouldSendEvent = ^BOOL(SentryEvent * _Nonnull event) {
192-
// We don't want to send an event after startup that came from a NSException of react native
193-
// Because we sent it already before the app crashed.
194-
if (nil != event.exceptions.firstObject.type &&
195-
[event.exceptions.firstObject.type rangeOfString:@"RCTFatalException"].location != NSNotFound) {
196-
NSLog(@"RCTFatalException");
197-
return NO;
184+
static dispatch_once_t onceStartToken;
185+
dispatch_once(&onceStartToken, ^{
186+
NSError *error = nil;
187+
SentryClient *client = [[SentryClient alloc] initWithDsn:dsnString didFailWithError:&error];
188+
[SentryClient setSharedClient:client];
189+
[SentryClient.sharedClient startCrashHandlerWithError:&error];
190+
if (error) {
191+
[NSException raise:@"SentryReactNative" format:@"%@", error.localizedDescription];
198192
}
199-
return YES;
200-
};
201-
SentryClient.sharedClient.beforeSerializeEvent = ^(SentryEvent * _Nonnull event) {
202-
[self injectReactNativeFrames:event];
203-
[self setReleaseVersionDist:event];
204-
};
193+
SentryClient.sharedClient.shouldSendEvent = ^BOOL(SentryEvent * _Nonnull event) {
194+
// We don't want to send an event after startup that came from a NSException of react native
195+
// Because we sent it already before the app crashed.
196+
if (nil != event.exceptions.firstObject.type &&
197+
[event.exceptions.firstObject.type rangeOfString:@"RCTFatalException"].location != NSNotFound) {
198+
NSLog(@"RCTFatalException");
199+
return NO;
200+
}
201+
return YES;
202+
};
203+
SentryClient.sharedClient.beforeSerializeEvent = ^(SentryEvent * _Nonnull event) {
204+
[self injectReactNativeFrames:event];
205+
[self setReleaseVersionDist:event];
206+
};
207+
});
205208
}
206209

207210
RCT_EXPORT_METHOD(activateStacktraceMerging:(RCTPromiseResolveBlock)resolve

0 commit comments

Comments
 (0)