Package rx.subjects

Class Subject<T,R>

java.lang.Object
rx.Observable<R>
rx.subjects.Subject<T,R>
Type Parameters:
T - the input value type
R - the output value type
All Implemented Interfaces:
Observer<T>
Direct Known Subclasses:
AsyncSubject, BehaviorSubject, BufferUntilSubscriber, PublishSubject, ReplaySubject, SerializedSubject, TestSubject, UnicastSubject

public abstract class Subject<T,R> extends Observable<R> implements Observer<T>
Represents an object that is both an Observable and an Observer.
  • Constructor Details

  • Method Details

    • hasObservers

      public abstract boolean hasObservers()
      Indicates whether the Subject has Observers subscribed to it.
      Returns:
      true if there is at least one Observer subscribed to this Subject, false otherwise
    • toSerialized

      public final SerializedSubject<T,R> toSerialized()
      Wraps a Subject so that it is safe to call its various on methods from different threads.

      When you use an ordinary Subject as a Subscriber, you must take care not to call its Observer.onNext(T) method (or its other on methods) from multiple threads, as this could lead to non-serialized calls, which violates the Observable contract and creates an ambiguity in the resulting Subject.

      To protect a Subject from this danger, you can convert it into a SerializedSubject with code like the following:

      
       mySafeSubject = myUnsafeSubject.toSerialized();
       
      Returns:
      SerializedSubject wrapping the current Subject