Skip to content

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

@ursusursus

Description

@ursusursus

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions