public abstract class MainThreadDisposable
extends java.lang.Object
implements io.reactivex.disposables.Disposable
Instances of this class are useful in creating observables which interact with APIs that can only be used on the main thread, such as UI objects.
A convenience method is also provided for validating whether code
is being called on the main thread. Calls to this method along with instances of this class are
commonly used when creating custom observables using the following pattern:
@Override public void subscribe(Observer extends T> o) {
MainThreadDisposable.verifyMainThread();
// TODO setup behavior
o.onSubscribe(new MainThreadDisposable() {
@Override protected void onDispose() {
// TODO undo behavior
}
});
}
| Constructor and Description |
|---|
MainThreadDisposable() |
| Modifier and Type | Method and Description |
|---|---|
void |
dispose() |
boolean |
isDisposed() |
protected abstract void |
onDispose() |
static void |
verifyMainThread()
Verify that the calling thread is the Android main thread.
|
public static void verifyMainThread()
Calls to this method are usually preconditions for subscription behavior which instances of this class later undo. See the class documentation for an example.
java.lang.IllegalStateException - when called from any other thread.public final boolean isDisposed()
isDisposed in interface io.reactivex.disposables.Disposablepublic final void dispose()
dispose in interface io.reactivex.disposables.Disposableprotected abstract void onDispose()