From 22d21844e5730e8e10725dc32d35af1c9b8a8d23 Mon Sep 17 00:00:00 2001 From: Gunter Spranz Date: Tue, 22 Mar 2016 19:52:07 -0700 Subject: [PATCH 1/5] Try tix the Catalyst instance crash 2nd try with postDelay on main thread --- .../dispatcher/rnbranch/RNBranchModule.java | 56 ++++++++++++++----- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java b/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java index 1fcfb15..7bbe336 100644 --- a/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java +++ b/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java @@ -236,21 +236,51 @@ private Branch.BranchLinkShareListener init(Callback callback) { public void sendRNEvent(String eventName, @Nullable WritableMap params) { // This should avoid the crash in getJSModule() at startup - // Todo: It would be even better to wait until the catalyst instance has became available, but its not clear - // how to do that. // See also: https://github.com/walmartreact/react-native-orientation-listener/issues/8 - // We also wait 100ms here before sending the event so the catalyst instance should be available. - try { - Thread.sleep(100); - if (getReactApplicationContext().hasActiveCatalystInstance()) { - getReactApplicationContext() - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit(eventName, params); - } - } catch(InterruptedException ex) { - Thread.currentThread().interrupt(); - } + ReactApplicationContext context = getReactApplicationContext(); + Handler mainHandler = new Handler(context.getMainLooper()); + + Runnable poller = new Runnable() { + + private Runnable init(ReactApplicationContext _context, Handler _mainHandler, String _eventName, WritableMap _params) { + mMainHandler = _mainHandler; + mEventName = _eventName; + mParams = _params; + + return this; + } + + final int pollDelayInMs = 50; + final int maxTries = 20; + + int tries = 0; + String mEventName; + WritableMap mParams; + Handler mMainHandler; + ReactApplicationContext mContext; + + @Override + public void run() { + try { + if (mContext.hasActiveCatalystInstance()) { + mContext + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit(mEventName, mParams); + } else { + tries++; + if (tries < maxTries) { + mMainHandler.postDelayed(this, pollDelayInMs); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + } + }.init(context, mainHandler, eventName, params); + + mainHandler.post(poller); } private static Object getReadableMapObjectForKey(ReadableMap readableMap, String key) { From 8df0feabfc68d476fa67ce7244b34ed5d7ceac0f Mon Sep 17 00:00:00 2001 From: Gunter Spranz Date: Wed, 23 Mar 2016 09:45:38 -0700 Subject: [PATCH 2/5] Try tix the Catalyst instance crash 2nd try with postDelay on main thread --- .../java/com/dispatcher/rnbranch/RNBranchModule.java | 12 ++++++++++-- package.json | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java b/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java index 7bbe336..fae3c47 100644 --- a/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java +++ b/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java @@ -59,6 +59,8 @@ private Branch.BranchReferralInitListener init(ReactActivity activity) { public RNBranchModule(ReactApplicationContext reactContext) { super(reactContext); + Log.d(REACT_CLASS, "ctor"); + forwardInitSessionFinishedEventToReactNative(reactContext); } @@ -246,15 +248,15 @@ public void sendRNEvent(String eventName, @Nullable WritableMap params) { private Runnable init(ReactApplicationContext _context, Handler _mainHandler, String _eventName, WritableMap _params) { mMainHandler = _mainHandler; mEventName = _eventName; + mContext = _context; mParams = _params; - return this; } final int pollDelayInMs = 50; final int maxTries = 20; - int tries = 0; + int tries = 1; String mEventName; WritableMap mParams; Handler mMainHandler; @@ -263,7 +265,9 @@ private Runnable init(ReactApplicationContext _context, Handler _mainHandler, St @Override public void run() { try { + Log.d(REACT_CLASS, "Catalyst instance poller try " + Integer.toString(tries)); if (mContext.hasActiveCatalystInstance()) { + Log.d(REACT_CLASS, "Catalyst instance active"); mContext .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit(mEventName, mParams); @@ -271,6 +275,8 @@ public void run() { tries++; if (tries < maxTries) { mMainHandler.postDelayed(this, pollDelayInMs); + } else { + Log.e(REACT_CLASS, "Could not get Catalyst instance"); } } } @@ -280,6 +286,8 @@ public void run() { } }.init(context, mainHandler, eventName, params); + Log.d(REACT_CLASS, "sendRNEvent"); + mainHandler.post(poller); } diff --git a/package.json b/package.json index 1fa9850..3cea8fd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-branch", - "version": "0.0.10", + "version": "0.0.11", "description": "Native Wrapper around Branch Metrics native SDKs", "main": "index.js", "scripts": { From d1700b9d5a091afa12103610cbe16db33085751b Mon Sep 17 00:00:00 2001 From: Gunter Spranz Date: Wed, 23 Mar 2016 09:47:14 -0700 Subject: [PATCH 3/5] Try tix the Catalyst instance crash 2nd try with postDelay on main thread --- .../src/main/java/com/dispatcher/rnbranch/RNBranchModule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java b/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java index fae3c47..fffbbe7 100644 --- a/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java +++ b/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java @@ -253,7 +253,7 @@ private Runnable init(ReactApplicationContext _context, Handler _mainHandler, St return this; } - final int pollDelayInMs = 50; + final int pollDelayInMs = 100; final int maxTries = 20; int tries = 1; From 83ff453f1847936a54b239b10b40778ec0a4d62f Mon Sep 17 00:00:00 2001 From: Gunter Spranz Date: Wed, 23 Mar 2016 09:59:49 -0700 Subject: [PATCH 4/5] Try tix the Catalyst instance crash 2nd try with postDelay on main thread --- .../src/main/java/com/dispatcher/rnbranch/RNBranchModule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java b/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java index fffbbe7..0bdd167 100644 --- a/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java +++ b/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java @@ -273,7 +273,7 @@ public void run() { .emit(mEventName, mParams); } else { tries++; - if (tries < maxTries) { + if (tries <= maxTries) { mMainHandler.postDelayed(this, pollDelayInMs); } else { Log.e(REACT_CLASS, "Could not get Catalyst instance"); From c33387d6b04225d8f9e71530473a4b38c74740bb Mon Sep 17 00:00:00 2001 From: Gunter Spranz Date: Wed, 23 Mar 2016 10:26:18 -0700 Subject: [PATCH 5/5] Try for 30s --- .../main/java/com/dispatcher/rnbranch/RNBranchModule.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java b/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java index 0bdd167..8d4aa92 100644 --- a/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java +++ b/android/src/main/java/com/dispatcher/rnbranch/RNBranchModule.java @@ -35,7 +35,8 @@ public static void initSession(Uri uri, ReactActivity reactActivity) { private ReactActivity mActivity = null; @Override - public void onInitFinished(JSONObject referringParams, BranchError error) { + public void onInitFinished(JSONObject referringParams, BranchError error) { + Log.d(REACT_CLASS, "onInitFinished"); JSONObject result = new JSONObject(); try{ result.put("params", referringParams != null ? referringParams : JSONObject.NULL); @@ -254,7 +255,7 @@ private Runnable init(ReactApplicationContext _context, Handler _mainHandler, St } final int pollDelayInMs = 100; - final int maxTries = 20; + final int maxTries = 300; int tries = 1; String mEventName;