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

The following are Jave code examples for showing how to use subscribeOn() 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: SimpleMarkdown   File: EditFragment.java   Source Code and License Vote up 6 votes
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_edit, container, false);
    unbinder = ButterKnife.bind(this, view);
    ((MarkdownApplication) getActivity().getApplication()).getComponent().inject(this);
    Observable<String> obs = RxTextView.textChanges(markdownEditor)
            .debounce(50, TimeUnit.MILLISECONDS).map(CharSequence::toString);
    obs.subscribeOn(Schedulers.io());
    obs.observeOn(AndroidSchedulers.mainThread());
    obs.subscribe(markdown -> {
        presenter.onMarkdownEdited(markdown);
    });
    return view;
}
 
Example 2
Project: GitHub   File: RxJava2CallAdapter.java   Source Code and License Vote up 5 votes
@Override public Object adapt(Call<R> call) {
  Observable<Response<R>> responseObservable = isAsync
      ? new CallEnqueueObservable<>(call)
      : new CallExecuteObservable<>(call);

  Observable<?> observable;
  if (isResult) {
    observable = new ResultObservable<>(responseObservable);
  } else if (isBody) {
    observable = new BodyObservable<>(responseObservable);
  } else {
    observable = responseObservable;
  }

  if (scheduler != null) {
    observable = observable.subscribeOn(scheduler);
  }

  if (isFlowable) {
    return observable.toFlowable(BackpressureStrategy.LATEST);
  }
  if (isSingle) {
    return observable.singleOrError();
  }
  if (isMaybe) {
    return observable.singleElement();
  }
  if (isCompletable) {
    return observable.ignoreElements();
  }
  return observable;
}
 
Example 3
Project: GitHub   File: RxJava2CallAdapter.java   Source Code and License Vote up 5 votes
@Override public Object adapt(Call<R> call) {
  Observable<Response<R>> responseObservable = isAsync
      ? new CallEnqueueObservable<>(call)
      : new CallExecuteObservable<>(call);

  Observable<?> observable;
  if (isResult) {
    observable = new ResultObservable<>(responseObservable);
  } else if (isBody) {
    observable = new BodyObservable<>(responseObservable);
  } else {
    observable = responseObservable;
  }

  if (scheduler != null) {
    observable = observable.subscribeOn(scheduler);
  }

  if (isFlowable) {
    return observable.toFlowable(BackpressureStrategy.LATEST);
  }
  if (isSingle) {
    return observable.singleOrError();
  }
  if (isMaybe) {
    return observable.singleElement();
  }
  if (isCompletable) {
    return observable.ignoreElements();
  }
  return observable;
}
 
Example 4
Project: retrofit-rxjava-request-with-progress   File: RxJava2WithProgressCallAdapter.java   Source Code and License Vote up 5 votes
@Override public Object adapt(Call<R> call) {

    if (call.request().body() == null) {
        throw new IllegalStateException("can't execute requestbody progress task, specify request not exist requestbody: " + call.request());
    }

    Observable<ProgressBean<Response<R>>> responseObservable = isAsync
        ? new CallEnqueueWithProgressObservable<>(call)
        : new CallExecuteWithProgressObservable<>(call);

    Observable<?> observable;
    if (isResult) {
      observable = new ResultWithProgressObservable<>(responseObservable);
    } else if (isBody) {
      observable = new BodyWithProgressObservable<>(responseObservable);
    } else {
      observable = responseObservable;
    }

    if (scheduler != null) {
      observable = observable.subscribeOn(scheduler);
    }

    if (isFlowable) {
      return observable.toFlowable(BackpressureStrategy.LATEST);
    }
    if (isSingle) {
      return observable.singleOrError();
    }
    if (isMaybe) {
      return observable.singleElement();
    }
    if (isCompletable) {
      return observable.ignoreElements();
    }
    return observable;
  }
 
Example 5
Project: super-volley   File: RxJava2CallAdapter.java   Source Code and License Vote up 5 votes
@Override
public Object adapt(Call<R> call) {
    Observable<Response<R>> responseObservable = new CallObservable<>(call);

    Observable<?> observable;
    if (isResult) {
        observable = new ResultObservable<>(responseObservable);
    } else if (isBody) {
        observable = new BodyObservable<>(responseObservable);
    } else {
        observable = responseObservable;
    }

    if (scheduler != null) {
        observable = observable.subscribeOn(scheduler);
    }

    if (isFlowable) {
        return observable.toFlowable(BackpressureStrategy.LATEST);
    }
    if (isSingle) {
        return observable.singleOrError();
    }
    if (isMaybe) {
        return observable.singleElement();
    }
    if (isCompletable) {
        return observable.ignoreElements();
    }
    return observable;
}
 
Example 6
Project: RxNetwork   File: RxNetwork.java   Source Code and License Vote up 5 votes
/**
 * RxNetworkInfo connectivity observable with all the original {@link NetworkInfo} information
 * that uses custom defined {@link NetworkObservingStrategy strategy}.
 *
 * @param strategy custom {@link NetworkObservingStrategy} instance
 *
 * @return RxJava Observable with {@link RxNetworkInfo} containing network information
 */
@NonNull
@RequiresPermission(ACCESS_NETWORK_STATE)
public Observable<RxNetworkInfo> observe(@NonNull NetworkObservingStrategy strategy) {
  checkNotNullWithMessage(strategy, "Please provide network observing strategy or initialize"
      + " RxNetwork with proper Context to use the default one");

  final Observable<RxNetworkInfo> observable = strategy.observe();

  if (scheduler != null) {
    observable.subscribeOn(scheduler);
  }

  return observable;
}
 
Example 7
Project: RxNetwork   File: RxNetwork.java   Source Code and License Vote up 5 votes
/**
 * Real internet access observable with custom defined {@link InternetObservingStrategy strategy}.
 *
 * @param strategy custom {@link InternetObservingStrategy} instance
 *
 * @return RxJava Observable with {@code Boolean} ( {@code true} if there is real internet access,
 * {@code false} if not )
 */
@NonNull
@RequiresPermission(INTERNET)
public Observable<Boolean> observeInternetAccess(@NonNull InternetObservingStrategy strategy) {
  checkNotNull(strategy, "internet observing strategy");

  final Observable<Boolean> observable = strategy.observe();
  if (scheduler != null) {
    observable.subscribeOn(scheduler);
  }
  return observable;
}
 
Example 8
Project: buckaroo   File: DownloadTask.java   Source Code and License Vote up 4 votes
public static Observable<DownloadProgress> download(final URI url, final OutputStream output) {

        Preconditions.checkNotNull(url);
        Preconditions.checkNotNull(output);

        final Observable<DownloadProgress> observable = Observable.create(emitter -> {

            final OkHttpClient client = new OkHttpClient.Builder()
                .followRedirects(true)
                .followSslRedirects(true)
                .build();

            final Request request = new Request.Builder()
                .url(url.toURL())
                .build();

            final Response response = client.newCall(request).execute();

            try {

                if (!response.isSuccessful()) {
                    throw new IOException("Unexpected code " + response);
                }

                final InputStream input = new BufferedInputStream(response.body().byteStream());

                final byte[] data = new byte[1024];
                final long contentLength = response.body().contentLength();

                long total = 0;

                if (!emitter.isDisposed()) {
                    emitter.onNext(DownloadProgress.of(total, contentLength));
                }

                int count;
                long lastCount = 0;
                long lastEmissionCount = 0;

                while ((count = input.read(data)) != -1) {

                    if (emitter.isDisposed()) {
                        break;
                    }

                    total += count;
                    output.write(data, 0, count);

                    if ((total - lastCount) >= PROGRESS_REPORT_EVERY_N_BYTES) {
                        lastEmissionCount = total;
                        emitter.onNext(DownloadProgress.of(total, contentLength));
                    }

                    lastCount = total;
                }

                output.flush();
                output.close();

                input.close();

                if (emitter.isDisposed()) {
                    return;
                }

                if (lastEmissionCount != total) {
                    emitter.onNext(DownloadProgress.of(total, contentLength));
                }

                emitter.onComplete();
            } catch (final Throwable e) {
                if (!emitter.isDisposed()) {
                    emitter.onError(e);
                }
            }
        });

        return observable.subscribeOn(Schedulers.io());
    }