From 43ff9b4252e7da2614a8450eae1b1a1f8564cf1b Mon Sep 17 00:00:00 2001 From: Aaron Chiu Date: Fri, 4 Aug 2017 01:52:09 -0700 Subject: [PATCH] run onReactContextInitialized() on the UIThread Reviewed By: furdei Differential Revision: D5560722 fbshipit-source-id: 9871761dc5b314776c27128cfc51e0a414f9a736 --- .../facebook/react/ReactInstanceManager.java | 72 ++++++++++--------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index 84f0ce95146648..e0060936052a7c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -9,13 +9,25 @@ package com.facebook.react; -import javax.annotation.Nullable; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; +import static com.facebook.infer.annotation.ThreadConfined.UI; +import static com.facebook.react.bridge.ReactMarkerConstants.ATTACH_MEASURED_ROOT_VIEWS_END; +import static com.facebook.react.bridge.ReactMarkerConstants.ATTACH_MEASURED_ROOT_VIEWS_START; +import static com.facebook.react.bridge.ReactMarkerConstants.BUILD_NATIVE_MODULE_REGISTRY_END; +import static com.facebook.react.bridge.ReactMarkerConstants.BUILD_NATIVE_MODULE_REGISTRY_START; +import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_CATALYST_INSTANCE_END; +import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_CATALYST_INSTANCE_START; +import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_REACT_CONTEXT_START; +import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_VIEW_MANAGERS_END; +import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_VIEW_MANAGERS_START; +import static com.facebook.react.bridge.ReactMarkerConstants.PRE_SETUP_REACT_CONTEXT_END; +import static com.facebook.react.bridge.ReactMarkerConstants.PRE_SETUP_REACT_CONTEXT_START; +import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_PACKAGES_END; +import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_PACKAGES_START; +import static com.facebook.react.bridge.ReactMarkerConstants.SETUP_REACT_CONTEXT_END; +import static com.facebook.react.bridge.ReactMarkerConstants.SETUP_REACT_CONTEXT_START; +import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_APPS; +import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; +import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JSC_CALLS; import android.app.Activity; import android.content.Context; @@ -24,7 +36,6 @@ import android.os.Process; import android.util.Log; import android.view.View; - import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; import com.facebook.infer.annotation.ThreadConfined; @@ -67,26 +78,12 @@ import com.facebook.soloader.SoLoader; import com.facebook.systrace.Systrace; import com.facebook.systrace.SystraceMessage; - -import static com.facebook.infer.annotation.ThreadConfined.UI; -import static com.facebook.react.bridge.ReactMarkerConstants.ATTACH_MEASURED_ROOT_VIEWS_END; -import static com.facebook.react.bridge.ReactMarkerConstants.ATTACH_MEASURED_ROOT_VIEWS_START; -import static com.facebook.react.bridge.ReactMarkerConstants.BUILD_NATIVE_MODULE_REGISTRY_END; -import static com.facebook.react.bridge.ReactMarkerConstants.BUILD_NATIVE_MODULE_REGISTRY_START; -import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_CATALYST_INSTANCE_END; -import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_CATALYST_INSTANCE_START; -import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_REACT_CONTEXT_START; -import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_VIEW_MANAGERS_END; -import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_VIEW_MANAGERS_START; -import static com.facebook.react.bridge.ReactMarkerConstants.PRE_SETUP_REACT_CONTEXT_END; -import static com.facebook.react.bridge.ReactMarkerConstants.PRE_SETUP_REACT_CONTEXT_START; -import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_PACKAGES_END; -import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_PACKAGES_START; -import static com.facebook.react.bridge.ReactMarkerConstants.SETUP_REACT_CONTEXT_END; -import static com.facebook.react.bridge.ReactMarkerConstants.SETUP_REACT_CONTEXT_START; -import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_APPS; -import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; -import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JSC_CALLS; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import javax.annotation.Nullable; /** * This class is managing instances of {@link CatalystInstance}. It exposes a way to configure @@ -839,7 +836,7 @@ public void run() { mCreateReactContextThread.start(); } - private void setupReactContext(ReactApplicationContext reactContext) { + private void setupReactContext(final ReactApplicationContext reactContext) { Log.d(ReactConstants.TAG, "ReactInstanceManager.setupReactContext()"); ReactMarker.logMarker(PRE_SETUP_REACT_CONTEXT_END); ReactMarker.logMarker(SETUP_REACT_CONTEXT_START); @@ -863,11 +860,18 @@ private void setupReactContext(ReactApplicationContext reactContext) { ReactInstanceEventListener[] listeners = new ReactInstanceEventListener[mReactInstanceEventListeners.size()]; - listeners = mReactInstanceEventListeners.toArray(listeners); - - for (ReactInstanceEventListener listener : listeners) { - listener.onReactContextInitialized(reactContext); - } + final ReactInstanceEventListener[] finalListeners = + mReactInstanceEventListeners.toArray(listeners); + + UiThreadUtil.runOnUiThread( + new Runnable() { + @Override + public void run() { + for (ReactInstanceEventListener listener : finalListeners) { + listener.onReactContextInitialized(reactContext); + } + } + }); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(SETUP_REACT_CONTEXT_END); mCurrentReactContext.runOnJSQueueThread(new Runnable() {