Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UiThreadUtil in android (#493) #531

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JuliaSpehrer
Copy link

Fix for #493.

Without this fix this error is shown:
image

With the fix the error is gone.

Solution found in the edit of the initial question here: https://stackoverflow.com/questions/60506273/react-native-android-bridge-error-must-be-called-on-main-thread

@tafty
Copy link

tafty commented Jan 13, 2025

Will this PR ever be merged? I applied it as a patch in my project today and can confirm it fixes the issue described (though in my case the app completely hung too).

@wfern
Copy link

wfern commented Jan 21, 2025

I think this PR may cause some problems, like this issue: #321

The background thread (executor) was added to offload the work out of the ui thread: #317 and #472

I'm using Fabric. In my case besides showing the error, the capture is generated successfully because the error is only a assert that shows in debug mode: https://github.com/facebook/react-native/blob/9afad527b831ec0c5d50e88daacbaacbc476d478/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java#L925

Maybe we can resolve the view in the ui thread and do the rest of the work in a separated thread

After digging further I understood that the ViewShot class implements UIBlock interface and it is dispatched to the UI manager queue so in this case there would be no need for runOnUiThread. So we just need to execute the rest of the code in the executor.

I made this patch taking out the view resolver out of the executor:

diff --git a/node_modules/react-native-view-shot/android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java b/node_modules/react-native-view-shot/android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java
index ce81dda..7b7332b 100644
--- a/node_modules/react-native-view-shot/android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java
+++ b/node_modules/react-native-view-shot/android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java
@@ -195,26 +195,26 @@ public class ViewShot implements UIBlock, com.facebook.react.fabric.interop.UIBl
 
     //region Implementation
     private void executeImpl(final NativeViewHierarchyManager nativeViewHierarchyManager, final UIBlockViewResolver uiBlockViewResolver) {
+        final View view;
+
+        if (tag == -1) {
+            view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
+        } else if (uiBlockViewResolver != null) {
+            view = uiBlockViewResolver.resolveView(tag);
+        } else {
+            view = nativeViewHierarchyManager.resolveView(tag);
+        }
+
+        if (view == null) {
+            Log.e(TAG, "No view found with reactTag: " + tag, new AssertionError());
+            promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: " + tag);
+            return;
+        }
+
         executor.execute(new Runnable () {
             @Override
             public void run() {
                 try {
-                    final View view;
-
-                    if (tag == -1) {
-                        view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
-                    } else if (uiBlockViewResolver != null) {
-                        view = uiBlockViewResolver.resolveView(tag);
-                    } else {
-                        view = nativeViewHierarchyManager.resolveView(tag);
-                    }
-
-                    if (view == null) {
-                        Log.e(TAG, "No view found with reactTag: " + tag, new AssertionError());
-                        promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: " + tag);
-                        return;
-                    }
-
                     final ReusableByteArrayOutputStream stream = new ReusableByteArrayOutputStream(outputBuffer);
                     stream.setSize(proposeSize(view));
                     outputBuffer = stream.innerBuffer();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants