Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Commit

Permalink
Ensure main thread when getting instanceManager (wix#681)
Browse files Browse the repository at this point in the history
(cherry picked from commit a9b204e)
  • Loading branch information
wiyarmir committed Apr 23, 2018
1 parent 5e51d2c commit c81b9c2
Showing 1 changed file with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,33 @@ static boolean isReactNativeApp() {
*/
public static Object getInstanceManager(@NonNull Object reactNativeHostHolder) {
Object instanceManager = null;
try {
instanceManager = Reflect.on(reactNativeHostHolder)
.call(METHOD_GET_RN_HOST)
.call(METHOD_GET_INSTANCE_MANAGER)
.get();
} catch (ReflectException e) {
Log.e(LOG_TAG, "Problem calling getInstanceManager()", e.getCause());
// If we are not in main thread and instanceManager has not been created, creation will fail
if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
try {
instanceManager = Reflect.on(reactNativeHostHolder)
.call(METHOD_GET_RN_HOST)
.call(METHOD_GET_INSTANCE_MANAGER)
.get();
} catch (ReflectException e) {
Log.e(LOG_TAG, "Problem calling getInstanceManager()", e.getCause());
}
} else {
instanceManager = getInstanceManagerFromOtherThread(reactNativeHostHolder);
}

return instanceManager;
}

private static Object getInstanceManagerFromOtherThread(@NonNull final Object reactNativeHostHolder) {
final Object[] instanceManager = {null};
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
instanceManager[0] = getInstanceManager(reactNativeHostHolder);
}
});
return instanceManager[0];
}

/**
* <p>
* Reloads the React Native application.
Expand Down

0 comments on commit c81b9c2

Please sign in to comment.