Package rx.subjects

Class Subject<T,​R>

    • Method Detail

      • 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