1

In RxJava what is the difference between Emitter interface and Observer interface ? both have same methods

public interface Emitter<@NonNull T> {

    /**
     * Signal a normal value.
     * @param value the value to signal, not {@code null}
     */
    void onNext(@NonNull T value);

    /**
     * Signal a {@link Throwable} exception.
     * @param error the {@code Throwable} to signal, not {@code null}
     */
    void onError(@NonNull Throwable error);

    /**
     * Signal a completion.
     */
    void onComplete();
}


public interface Observer<@NonNull T> {

    /**
     * Provides the {@link Observer} with the means of cancelling (disposing) the
     * connection (channel) with the {@link Observable} in both
     * synchronous (from within {@link #onNext(Object)}) and asynchronous manner.
     * @param d the {@link Disposable} instance whose {@link Disposable#dispose()} can
     * be called anytime to cancel the connection
     * @since 2.0
     */
    void onSubscribe(@NonNull Disposable d);

    /**
     * Provides the {@link Observer} with a new item to observe.
     * <p>
     * The {@link Observable} may call this method 0 or more times.
     * <p>
     * The {@code Observable} will not call this method again after it calls either {@link #onComplete} or
     * {@link #onError}.
     *
     * @param t
     *          the item emitted by the Observable
     */
    void onNext(@NonNull T t);

    /**
     * Notifies the {@link Observer} that the {@link Observable} has experienced an error condition.
     * <p>
     * If the {@code Observable} calls this method, it will not thereafter call {@link #onNext} or
     * {@link #onComplete}.
     *
     * @param e
     *          the exception encountered by the Observable
     */
    void onError(@NonNull Throwable e);

    /**
     * Notifies the {@link Observer} that the {@link Observable} has finished sending push-based notifications.
     * <p>
     * The {@code Observable} will not call this method if it calls {@link #onError}.
     */
    void onComplete();

}
Bravo
  • 8,589
  • 14
  • 48
  • 85

2 Answers2

1

both have same methods

No. Observer.onSubscribe is deliberately not present in Emitter.

Emitter is an interface to allow a callback to signal one item, one error or complete. It is used in the generate operator to produce the next signal.

Since generate handles cancellation inbetween invocation of the callback, there is no reason or means to call onSubscribe of an Observer, thus to avoid confusion and to have a clean minimal API, the Observer interface cannot be used and a dedicated interface, Emitter, has been added instead.

akarnokd
  • 69,132
  • 14
  • 157
  • 192
0

An Emitter calls its onNext when there is something new. An Observer expects its onNext to be called when there is something new.

The Emitter knows about events and can expose them by calling its onNext. An Observer wants to be told about events and will learn about them when its onNext is called.

John
  • 1,322
  • 14
  • 26