runOnUiThread - purpose

runOnUiThread - purpose

We have recently developed a game on Cocos2dx-JS.
Plugins were implemented in JAVA for Ironsource and Google play services and are called via jsb reflection.

Can someone tell us whether runOnUiThread has to be used on methods like showRewardedAds, signIn() or submitScore() ?

And if that is not used, what will be the default thread ?

Without specifying you always are in the main thread. Any cocos object needs to be dealt with from the main thread. I use other threads for dealing with non-cocos objects or updating properties of a cocos object that aren’t ui related.

@slackmoehrle I am able to see 2 threads, UI Thread & GL thread. I have the same doubt whether JS related calls to be done in GL thread? & non cocos object [example: android platform specific such as signin() to google play, facebook login()] to be done in UI Thread?
Which is Main Thread? UI Thread or GL Thread, or is it completely different thread?

@Zinitter Can you please clarify on this matter?

By main thread I meant ui thread

main UI thread is main Android app thread but for cocos2d engine the GL thread is main.

JS related calls should be made on GL thread (cocos2d thread).


sorry, i have been quite long time didn’t develop for android.

by common, those UI related action should using the Main Thread which is the UI Thread so can see response on screen.

i dig out my last time issue for your references, but this is about call js function from java.

On decompiling sdkbox play plugin and inspecting codes of other open-source projects, it was found that runOnUiThread method is being explicitly used on submitScore method of google play services/leaderboard.

One such example:

Is there any specific reason why it is being done so?

Got it @slackmoehrle:+1:

@Zinitter Understood, Thanks for reply :slight_smile:

@slackmoehrle I think you are wrong here. I just checked thread.currentthread() in the JAVA method which was called from JavaScript. It was found to be GLThread.

@zhangxm Please add your thoughts on why runOnUiThread is needed.

@vyshnav_s_deepak if it’s called from JavaScript then it’s called from GLThread which is cocos2d thread.
You should use runOnUiThread to execute Java code.

Being curious. @dimon4eg Can you tell me why it should be used (on all JS to JAVA calls) and what happens if not done that way?

Actually it depends what your Java code does.
To do not worry and be safe just call your code inside of runOnUiThread.