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

DispatchOnMainThreadException even if not on MainThread #1

Open
schlangguru opened this issue Jun 20, 2017 · 2 comments
Open

DispatchOnMainThreadException even if not on MainThread #1

schlangguru opened this issue Jun 20, 2017 · 2 comments

Comments

@schlangguru
Copy link

There seems to be an issue with the check if a dispatch happens on the main-thread. In the ThreadUtils class the check is like follows:

    public static boolean isOnMain() {
        return getId() != 1;//fingers crossed. This might be an issue.
    }

    public static void ensureNotOnMain(){
        if(isOnMain())
            throw new DispatchOnMainThreadException();
    }

Souldn´t the isOnMain method be implemented with getId() == 1? Saying if the id == 1 then we are on the main thread. In the current implementaton we return true only if we are on a thread an id other than 1.

Besides, does the main-thread check even make sense for a pure java implementation? Most UI libraries have teir own ui-thread (swings EventDispatchThread, or javaFXs application thread). But these threads will always differ from the thread with id 1.

@thedumbtechguy
Copy link
Owner

Nice catch. I'm not sure what I was smoking when I wrote that.

Concerning the thread id on java, I've always known this is an issue hence the comment.
I'll have to fix check for android and convert the check to a delegate in pure Java for custom implementations.

@thedumbtechguy
Copy link
Owner

thedumbtechguy commented Jun 24, 2017

So the check on Android is fine. It uses the Looper so that's why I've never faced this issue.

I'll convert this into a delegate that can be overridden. If you have any ideas, I'll also appreciate a pull.

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

No branches or pull requests

2 participants