From 5fb82e7425858020626d0cdee8ab7800c1b9bbf3 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Mon, 7 Dec 2020 13:39:10 -0800 Subject: [PATCH] Ensure ReactRootView.getId() is accessed on the UIThread Summary: Ensure ReactRootView.getId() is accessed on the UIThread changelog: [internal] intenral Reviewed By: JoshuaGross Differential Revision: D25321379 fbshipit-source-id: 889e59c655324352a7b9ac5bed769750786b8190 --- .../fabric/mounting/MountingManager.java | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java index 811393666c7..1dfeb478cb9 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java @@ -8,7 +8,6 @@ package com.facebook.react.fabric.mounting; import static com.facebook.infer.annotation.ThreadConfined.ANY; -import static com.facebook.infer.annotation.ThreadConfined.UI; import android.content.Context; import android.view.View; @@ -100,23 +99,29 @@ public class MountingManager { * @param reactRootTag * @param rootView */ - @ThreadConfined(UI) - public void addRootView(int reactRootTag, @NonNull View rootView) { - if (rootView.getId() != View.NO_ID) { - FLog.e( - TAG, - "Trying to add RootTag to RootView that already has a tag: existing tag: [%d] new tag: [%d]", - rootView.getId(), - reactRootTag); - throw new IllegalViewOperationException( - "Trying to add a root view with an explicit id already set. React Native uses " - + "the id field to track react tags and will overwrite this field. If that is fine, " - + "explicitly overwrite the id field to View.NO_ID before calling addRootView."); - } - + @AnyThread + public void addRootView(final int reactRootTag, @NonNull final View rootView) { mTagToViewState.put( reactRootTag, new ViewState(reactRootTag, rootView, mRootViewManager, true)); - rootView.setId(reactRootTag); + + UiThreadUtil.runOnUiThread( + new Runnable() { + @Override + public void run() { + if (rootView.getId() != View.NO_ID) { + FLog.e( + TAG, + "Trying to add RootTag to RootView that already has a tag: existing tag: [%d] new tag: [%d]", + rootView.getId(), + reactRootTag); + throw new IllegalViewOperationException( + "Trying to add a root view with an explicit id already set. React Native uses " + + "the id field to track react tags and will overwrite this field. If that is fine, " + + "explicitly overwrite the id field to View.NO_ID before calling addRootView."); + } + rootView.setId(reactRootTag); + } + }); } /** Delete rootView and all children/ */