public abstract class MainThreadSubscription extends java.lang.Object implements Subscription
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 call(Subscriber extends T> subscriber) {
MainThreadSubscription.verifyMainThread();
// TODO set up behavior
subscriber.add(new MainThreadSubscription() {
@Override public void onUnsubscribe() {
// TODO undo behavior
}
});
}
Constructor and Description |
---|
MainThreadSubscription() |
Modifier and Type | Method and Description |
---|---|
boolean |
isUnsubscribed() |
protected abstract void |
onUnsubscribe() |
void |
unsubscribe() |
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 isUnsubscribed()
isUnsubscribed
in interface Subscription
public final void unsubscribe()
unsubscribe
in interface Subscription
protected abstract void onUnsubscribe()