flurry / react-native-flurry-sdk

React Native Flurry SDK
Apache License 2.0
45 stars 13 forks source link

About the best practices to initialize flurry sdk #4

Closed CubeSugar closed 5 years ago

CubeSugar commented 5 years ago

Current Behavior (using js)

case 1: crash

By using getCurrentActivity method to initialize Flurry Sdk, app may crash in some rare situations. I can't reproduce in development.

The getCurrentActivity sometimes returns null. Some stacktrace like below:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at com.flurry.android.FlurryAgent.a(SourceFile:5297) at com.flurry.android.FlurryAgent$Builder.build(SourceFile:1529) at com.flurry.android.reactnative.FlurryModule.init(FlurryModule.java:66) at java.lang.reflect.Method.invoke(Method.java) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:160) at com.facebook.react.bridge.queue.NativeRunnable.run(NativeRunnable.java) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29) at android.os.Looper.loop(Looper.java:154) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192) at java.lang.Thread.run(Thread.java:7611)

FYI: #https://github.com/facebook/react-native/issues/18345

case 2: flurry session

Using getReactApplicationContext as context to initialiaze SDK will prevent app crash, but

will cause "D FlurryAgent: There is no active Flurry session. Adding this event to deferred queue and flush them when the session initializes. Event: Some Event".

See: #3

Best Practices (native codes)

Prefer to use only native codes ( like java / Objective-C ) rather than js to initialize Flurry SDK. See Doc#Initialize Flurry

poting-oath commented 5 years ago
poting-oath commented 5 years ago

One solution is, if current Activity is available, then use it, otherwise use Application context.

poting-oath commented 5 years ago

After investigated Flurry's lifecycle, the above solution should work consistent with.

We will include this fix in the next release.

CubeSugar commented 5 years ago

@poting-oath That sounds great!

poting-oath commented 5 years ago

@CubeSugar We just released new version 2.1.0 that contains this fix. Thanks a lot for your great feedback!