Java Code Examples for io.reactivex.Observable.subscribeWith()

The following are Jave code examples for showing how to use subscribeWith() of the io.reactivex.Observable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Learning-RxJava   File: Ch2_34.java   Source Code and License Vote up 7 votes
public static void main(String[] args) {
        Observable<Long> source =
                Observable.interval(1, TimeUnit.SECONDS);
        ResourceObserver<Long> myObserver = new
                ResourceObserver<Long>() {
                    @Override
                    public void onNext(Long value) {
                        System.out.println(value);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        System.out.println("Done!");
                    }
                };
//capture Disposable
        Disposable disposable = source.subscribeWith(myObserver);
    }
 
Example 2
Project: code-examples-android-expert   File: RxJavaUnitTest.java   Source Code and License Vote up 6 votes
@Test public void test(){
    Observable<Todo> todoObservable = Observable.create(new ObservableOnSubscribe<Todo>() {
        @Override
        public void subscribe(ObservableEmitter<Todo> emitter) throws Exception {
            try {
                List<Todo> todos = RxJavaUnitTest.this.getTodos();
                if (todos!=null){
                    throw new NullPointerException("todos was null");
                }
                for (Todo todo : todos) {
                    emitter.onNext(todo);
                }
                emitter.onComplete();
            } catch (Exception e) {
                emitter.onError(e);
            }
        }
    });
    TestObserver<Object> testObserver = new TestObserver<>();
    todoObservable.subscribeWith(testObserver);
    testObserver.assertError(NullPointerException.class);

}
 
Example 3
Project: RxEasyHttp   File: PostRequest.java   Source Code and License Vote up 5 votes
public <T> Disposable execute(CallBackProxy<? extends ApiResult<T>, T> proxy) {
    Observable<CacheResult<T>> observable = build().toObservable(generateRequest(), proxy);
    if (CacheResult.class != proxy.getCallBack().getRawType()) {
        return observable.compose(new ObservableTransformer<CacheResult<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<CacheResult<T>> upstream) {
                return upstream.map(new CacheResultFunc<T>());
            }
        }).subscribeWith(new CallBackSubsciber<T>(context, proxy.getCallBack()));
    } else {
        return observable.subscribeWith(new CallBackSubsciber<CacheResult<T>>(context, proxy.getCallBack()));
    }
}
 
Example 4
Project: RxEasyHttp   File: GetRequest.java   Source Code and License Vote up 5 votes
public <T> Disposable execute(CallBackProxy<? extends ApiResult<T>, T> proxy) {
    Observable<CacheResult<T>> observable = build().toObservable(apiManager.get(url, params.urlParamsMap), proxy);
    if (CacheResult.class != proxy.getCallBack().getRawType()) {
        return observable.compose(new ObservableTransformer<CacheResult<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<CacheResult<T>> upstream) {
                return upstream.map(new CacheResultFunc<T>());
            }
        }).subscribeWith(new CallBackSubsciber<T>(context, proxy.getCallBack()));
    } else {
        return observable.subscribeWith(new CallBackSubsciber<CacheResult<T>>(context, proxy.getCallBack()));
    }
}
 
Example 5
Project: RxEasyHttp   File: CustomRequest.java   Source Code and License Vote up 5 votes
public <T> Disposable call(Observable<T> observable, CallBackProxy<? extends ApiResult<T>, T> proxy) {
    Observable<CacheResult<T>> cacheobservable = build().toObservable(observable, proxy);
    if (CacheResult.class != proxy.getCallBack().getRawType()) {
        return cacheobservable.compose(new ObservableTransformer<CacheResult<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<CacheResult<T>> upstream) {
                return upstream.map(new CacheResultFunc<T>());
            }
        }).subscribeWith(new CallBackSubsciber<T>(context, proxy.getCallBack()));
    } else {
        return cacheobservable.subscribeWith(new CallBackSubsciber<CacheResult<T>>(context, proxy.getCallBack()));
    }
}
 
Example 6
Project: RxEasyHttp   File: PutRequest.java   Source Code and License Vote up 5 votes
public <T> Disposable execute(CallBackProxy<? extends ApiResult<T>, T> proxy) {
    Observable<CacheResult<T>> observable = build().toObservable(apiManager.put(url, params.urlParamsMap), proxy);
    if (CacheResult.class != proxy.getCallBack().getRawType()) {
        return observable.compose(new ObservableTransformer<CacheResult<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<CacheResult<T>> upstream) {
                return upstream.map(new CacheResultFunc<T>());
            }
        }).subscribeWith(new CallBackSubsciber<T>(context, proxy.getCallBack()));
    } else {
        return observable.subscribeWith(new CallBackSubsciber<CacheResult<T>>(context, proxy.getCallBack()));
    }
}
 
Example 7
Project: RxEasyHttp   File: DeleteRequest.java   Source Code and License Vote up 5 votes
public <T> Disposable execute(CallBackProxy<? extends ApiResult<T>, T> proxy) {
    Observable<CacheResult<T>> observable = build().toObservable(generateRequest(), proxy);
    if (CacheResult.class != proxy.getCallBack().getRawType()) {
        return observable.compose(new ObservableTransformer<CacheResult<T>, T>() {
            @Override
            public ObservableSource<T> apply(@NonNull Observable<CacheResult<T>> upstream) {
                return upstream.map(new CacheResultFunc<T>());
            }
        }).subscribeWith(new CallBackSubsciber<T>(context, proxy.getCallBack()));
    } else {
        return observable.subscribeWith(new CallBackSubsciber<CacheResult<T>>(context, proxy.getCallBack()));
    }
}
 
Example 8
Project: ZgzFromWithin   File: UseCase.java   Source Code and License Vote up 5 votes
public void executeObserver(DisposableObserver<T> observer, int from, int limit) {
    if (observer != null) {
        final Observable<T> observable = createObservableUseCase(from, limit).subscribeOn(executorSchedule).observeOn(uiSchedule);

        DisposableObserver disposableObserver = observable.subscribeWith(observer);
        compositeDisposable.add(disposableObserver);
    }
}
 
Example 9
Project: Subs   File: UseCase.java   Source Code and License Vote up 5 votes
/**
 * Executes the current use case.
 *
 * @param observer {@link DisposableObserver} which will be listening to the observable build
 * by {@link #buildUseCaseObservable(Params)} ()} method.
 * @param params Parameters (Optional) used to build/execute this use case.
 */
public Disposable execute(DisposableObserver<T> observer, Params params) {
    Preconditions.checkNotNull(observer);
    final Observable<T> observable = this.buildUseCaseObservable(params)
            .subscribeOn(Schedulers.from(threadExecutor))
            .observeOn(postExecutionThread.getScheduler());
    return observable.subscribeWith(observer);
}
 
Example 10
Project: GitHub   File: MviBasePresenter.java   Source Code and License Vote up 4 votes
/**
 * This method subscribes the Observable emitting {@code ViewState} over time to the passed
 * consumer.
 * <b>Do only invoke this method once! Typically in {@link #bindIntents()}</b>
 * <p>
 * Internally Mosby will hold some relays to ensure that no items emitted from the ViewState
 * Observable will be lost while viewState is not attached nor that the subscriptions to
 * viewState
 * intents will cause memory leaks while viewState detached.
 * </p>
 *
 * Typically this method is used in {@link #bindIntents()}  like this:
 * <pre><code>
 *   Observable<MyViewState> viewState =  ... ;
 *   subscribeViewStateConsumerActually(viewState, new ViewStateConsumer() {
 *      @Override
 *      public void accept(MyView view, MyViewState viewState){
 *         view.render(viewState);
 *      }
 *   }
 * </code></pre>
 *
 * @param viewStateObservable The Observable emitting new ViewState. Typically an intent {@link
 * #intent(ViewIntentBinder)} causes the underlying business logic to do a change and eventually
 * create a new ViewState.
 * @param consumer {@link ViewStateConsumer} The consumer that will update ("render") the view.
 */
@MainThread protected void subscribeViewState(@NonNull Observable<VS> viewStateObservable,
    @NonNull ViewStateConsumer<V, VS> consumer) {
  if (subscribeViewStateMethodCalled) {
    throw new IllegalStateException(
        "subscribeViewState() method is only allowed to be called once");
  }
  subscribeViewStateMethodCalled = true;

  if (viewStateObservable == null) {
    throw new NullPointerException("ViewState Observable is null");
  }

  if (consumer == null) {
    throw new NullPointerException("ViewStateBinder is null");
  }

  this.viewStateConsumer = consumer;

  viewStateDisposable = viewStateObservable.subscribeWith(
      new DisposableViewStateObserver<>(viewStateBehaviorSubject));
}
 
Example 11
Project: GitHub   File: MviBasePresenter.java   Source Code and License Vote up 4 votes
/**
 * This method subscribes the Observable emitting {@code ViewState} over time to the passed
 * consumer.
 * <b>Do only invoke this method once! Typically in {@link #bindIntents()}</b>
 * <p>
 * Internally Mosby will hold some relays to ensure that no items emitted from the ViewState
 * Observable will be lost while viewState is not attached nor that the subscriptions to
 * viewState
 * intents will cause memory leaks while viewState detached.
 * </p>
 *
 * Typically this method is used in {@link #bindIntents()}  like this:
 * <pre><code>
 *   Observable<MyViewState> viewState =  ... ;
 *   subscribeViewStateConsumerActually(viewState, new ViewStateConsumer() {
 *      @Override
 *      public void accept(MyView view, MyViewState viewState){
 *         view.render(viewState);
 *      }
 *   }
 * </code></pre>
 *
 * @param viewStateObservable The Observable emitting new ViewState. Typically an intent {@link
 * #intent(ViewIntentBinder)} causes the underlying business logic to do a change and eventually
 * create a new ViewState.
 * @param consumer {@link ViewStateConsumer} The consumer that will update ("render") the view.
 */
@MainThread protected void subscribeViewState(@NonNull Observable<VS> viewStateObservable,
    @NonNull ViewStateConsumer<V, VS> consumer) {
  if (subscribeViewStateMethodCalled) {
    throw new IllegalStateException(
        "subscribeViewState() method is only allowed to be called once");
  }
  subscribeViewStateMethodCalled = true;

  if (viewStateObservable == null) {
    throw new NullPointerException("ViewState Observable is null");
  }

  if (consumer == null) {
    throw new NullPointerException("ViewStateBinder is null");
  }

  this.viewStateConsumer = consumer;

  viewStateDisposable = viewStateObservable.subscribeWith(
      new DisposableViewStateObserver<>(viewStateBehaviorSubject));
}