Skip to content

Handler.post() only If not already on main thread? #423

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

Closed
ursusursus opened this issue Apr 29, 2018 · 1 comment
Closed

Handler.post() only If not already on main thread? #423

ursusursus opened this issue Apr 29, 2018 · 1 comment

Comments

@ursusursus
Copy link

ursusursus commented Apr 29, 2018

Hi,
Id like to ask, if its possible for HandlerScheduler to check wether it "needs to change threads", i.e. avoid posting if already on main thread. unnecessarily

This can manifest it self, when using observable mapped to Started, Success, Error events, and UI (main thread) subscribes to the observable (and Started is created via startWith()), which then posts the Started emit for no reason.

I know you can do this by rearranging the operators like

fooObservable.
.map( __ -> Success)
.onErrorReturn(t -> Error(t))
.observeOn(AndroidSchedulers.mainThread)
.startWith(Started))

But that ties it to main thread from from within of the api. I think in this case the subscriber should decide, (which we already do most of the time - which leaves the Started to be posted). Say if that was a network observable, and you want to compose 2 of them some how, which gives them implicit scheduler unneccesarily)

@JakeWharton
Copy link
Contributor

Dupe of #228. The answer is yes, it needs to post.

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