0

I'm writting a react-native native module using a third-party sdk, it report an error "xxxx should be used only from then application's main thread" when i init the sdk. How to create a native module in the application's main thread ?

1 Answer 1

1

The simple answer is that if you have any code that needs to be run on the main thread, you can use GCD in Objective-C to ensure that.

dispatch_async(dispatch_get_main_queue(), ^{
    // Code that needs to be run on the main thread.
});

By default, React Native runs all of it's code on a separate queue. In order to specify what queue your module runs on, React Native supplies the methodQueue function. For example, you could specify that all of your code in your module runs on the main thread:

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}

This would be helpful if your module is creating a native UI component, for example, and has to extensively call into UIKit methods.

Sign up to request clarification or add additional context in comments.

3 Comments

Right,i don't know about mehodQueue befor , i have serched some usage in the source code about how to use it in ios according to your answer , but few about how to use it in android. Do you know how to implement it in android , Is there any articals or example code i can reference. Thanks!
I found a solution to implement it in android. Handler handler = new Handler(context.getMainLooper()); handler.post(new Runable(){ // Code that needs to be run on the main thread. })
@user1557047 i was required of this method do you guys have nay documentation or something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.