Skip to content

Instantly share code, notes, and snippets.

@bwindels
Last active March 6, 2018 14:22
Show Gist options
  • Save bwindels/1c846c9da2d7be71a5446041c537f914 to your computer and use it in GitHub Desktop.
Save bwindels/1c846c9da2d7be71a5446041c537f914 to your computer and use it in GitHub Desktop.

How Rx Scheduling works

Given a chain of Rx operators, you can run certain parts of the chain on a different scheduler. SubscribeOn sets the initial scheduler of the chain, and when you call ObserveOn, the rest of the chain starts running on that scheduler, until you call ObserveOn again. You can call SubscribeOn at any point, and calling it multiple times makes little sense. ObserveOn applies to the rest of the chain from there on, so calling it multiple times makes a lot of sense. Nice diagram: http://reactivex.io/documentation/scheduler.html

Observable.From(...) ----------\
    v                          |
   Map                         |-- runs on threadpool
    v                          |
  Filter ----------------------/
    v
ObserveOn(UIScheduler)
    v
   Map --------------------------- runs on UIScheduler
SubscribeOn(threadPool)
    v
Subscribe();

You're really posting messages to different schedulers telling them to run a certain part of the chain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment