@Override public CompletableSource apply(Completable c) throws Exception { return c.subscribeOn(Schedulers.single()); } });
@Test(expected = NullPointerException.class) public void subscribeOnNull() { normal.completable.subscribeOn(null); }
public Completable updateJoin(final long playlistId, final List<Long> streamIds) { List<PlaylistStreamEntity> joinEntities = new ArrayList<>(streamIds.size()); for (int i = 0; i < streamIds.size(); i++) { joinEntities.add(new PlaylistStreamEntity(playlistId, streamIds.get(i), i)); } return Completable.fromRunnable(() -> database.runInTransaction(() -> { playlistStreamTable.deleteBatch(playlistId); playlistStreamTable.insertAll(joinEntities); })).subscribeOn(Schedulers.io()); }
private void doSomeWork() { Completable completable = Completable.timer(1000, TimeUnit.MILLISECONDS); completable .subscribeOn(Schedulers.io()) // Be notified on the main thread .observeOn(AndroidSchedulers.mainThread()) .subscribe(getCompletableObserver()); }
public void downloadFile(View view) { disposables.add(getCompletable() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(getDisposableObserver())); } }
@Test(timeout = 5000) public void subscribeOnNormal() { final AtomicReference<String> name = new AtomicReference<String>(); Completable c = Completable.unsafeCreate(new CompletableSource() { @Override public void subscribe(CompletableObserver observer) { name.set(Thread.currentThread().getName()); EmptyDisposable.complete(observer); } }).subscribeOn(Schedulers.computation()); c.blockingAwait(); Assert.assertTrue(name.get().startsWith("RxComputation")); }
@Override protected void doSomething() { mRxOperatorsText.append("Completable\n"); Log.e(TAG, "Completable\n"); Completable.timer(1, TimeUnit.SECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new CompletableObserver() { @Override public void onSubscribe(@NonNull Disposable d) { mRxOperatorsText.append("onSubscribe : d :" + d.isDisposed() + "\n"); Log.e(TAG, "onSubscribe : d :" + d.isDisposed() + "\n"); } @Override public void onComplete() { mRxOperatorsText.append("onComplete\n"); Log.e(TAG, "onComplete\n"); } @Override public void onError(@NonNull Throwable e) { mRxOperatorsText.append("onError :" + e.getMessage() + "\n"); Log.e(TAG, "onError :" + e.getMessage() + "\n"); } }); } }
private void updateSubscription(final ChannelInfo info) { if (DEBUG) Log.d(TAG, "updateSubscription() called with: info = [" + info + "]"); final Action onComplete = () -> { if (DEBUG) Log.d(TAG, "Updated subscription: " + info.getUrl()); }; final Consumer<Throwable> onError = (@NonNull Throwable throwable) -> onUnrecoverableError(throwable, UserAction.SUBSCRIPTION, NewPipe.getNameOfService(info.getServiceId()), "Updating Subscription for " + info.getUrl(), R.string.subscription_update_failed); disposables.add(subscriptionService.updateChannelInfo(info) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(onComplete, onError)); }
.subscribeOn(Schedulers.computation()) .subscribe(outer);
@Test(timeout = 5000) public void subscribeOnError() { final AtomicReference<String> name = new AtomicReference<String>(); Completable c = Completable.unsafeCreate(new CompletableSource() { @Override public void subscribe(CompletableObserver observer) { name.set(Thread.currentThread().getName()); EmptyDisposable.error(new TestException(), observer); } }).subscribeOn(Schedulers.computation()); try { c.blockingAwait(); Assert.fail("No exception thrown"); } catch (TestException ex) { // expected } Assert.assertTrue(name.get().startsWith("RxComputation")); }
.subscribeOn(Schedulers.io()) .observeOn(Schedulers.io()), c0)
.subscribeOn(Schedulers.io()) .observeOn(Schedulers.io()) .andThen(Completable.fromAction(new Action() {
/** * 让在前台的 {@link Activity}, 使用 {@link Snackbar} 显示文本内容 * * @param message * @param isLong */ public void showSnackbar(String message, boolean isLong) { if (getCurrentActivity() == null && getTopActivity() == null) { Timber.tag(TAG).w("mCurrentActivity == null when showSnackbar(String,boolean)"); return; } Completable.fromAction(new Action() { @Override public void run() throws Exception { //Arms 已将 com.android.support:design 从依赖中移除 (目的是减小 Arms 体积, design 库中含有太多 View) //因为 Snackbar 在 com.android.support:design 库中, 所以如果框架使用者没有自行依赖 com.android.support:design //Arms 则会使用 Toast 替代 Snackbar 显示信息, 如果框架使用者依赖了 arms-autolayout 库就不用依赖 com.android.support:design 了 //因为在 arms-autolayout 库中已经依赖有 com.android.support:design if (DEPENDENCY_SUPPORT_DESIGN) { Activity activity = getCurrentActivity() == null ? getTopActivity() : getCurrentActivity(); View view = activity.getWindow().getDecorView().findViewById(android.R.id.content); Snackbar.make(view, message, isLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT).show(); } else { ArmsUtils.makeText(mApplication, message); } } }).subscribeOn(AndroidSchedulers.mainThread()).subscribe(); }
@Test public void dispose() { TestHelper.checkDisposed(PublishSubject.create().ignoreElements().subscribeOn(new TestScheduler())); }
@Test public void noWinnerCompleteDispose() throws Exception { for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { final AtomicBoolean interrupted = new AtomicBoolean(); final CountDownLatch cdl = new CountDownLatch(1); Completable.ambArray( Completable.complete() .subscribeOn(Schedulers.single()) .observeOn(Schedulers.computation()), Completable.never() ) .subscribe(new Action() { @Override public void run() throws Exception { interrupted.set(Thread.currentThread().isInterrupted()); cdl.countDown(); } }); assertTrue(cdl.await(500, TimeUnit.SECONDS)); assertFalse("Interrupted!", interrupted.get()); } } }
@Test public void normal() { List<Throwable> list = TestHelper.trackPluginErrors(); try { TestScheduler scheduler = new TestScheduler(); TestObserver<Void> to = Completable.complete() .subscribeOn(scheduler) .test(); scheduler.advanceTimeBy(1, TimeUnit.SECONDS); to.assertResult(); assertTrue(list.toString(), list.isEmpty()); } finally { RxJavaPlugins.reset(); } }
@Test public void noWinnerErrorDispose() throws Exception { final TestException ex = new TestException(); for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { final AtomicBoolean interrupted = new AtomicBoolean(); final CountDownLatch cdl = new CountDownLatch(1); Completable.ambArray( Completable.error(ex) .subscribeOn(Schedulers.single()) .observeOn(Schedulers.computation()), Completable.never() ) .subscribe(Functions.EMPTY_ACTION, new Consumer<Throwable>() { @Override public void accept(Throwable e) throws Exception { interrupted.set(Thread.currentThread().isInterrupted()); cdl.countDown(); } }); assertTrue(cdl.await(500, TimeUnit.SECONDS)); assertFalse("Interrupted!", interrupted.get()); } }
@Test public void failSingleCompletableBlockingGet() { try { RxJavaPlugins.setFailOnNonBlockingScheduler(true); Completable.complete() .subscribeOn(Schedulers.single()) .doOnComplete(new Action() { @Override public void run() throws Exception { Completable.complete().delay(10, TimeUnit.SECONDS).blockingGet(); } }) .test() .awaitDone(5, TimeUnit.SECONDS) .assertFailure(IllegalStateException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void failSingleCompletableBlockingAwait() { try { RxJavaPlugins.setFailOnNonBlockingScheduler(true); Completable.complete() .subscribeOn(Schedulers.single()) .doOnComplete(new Action() { @Override public void run() throws Exception { Completable.complete().delay(10, TimeUnit.SECONDS).blockingAwait(); } }) .test() .awaitDone(5, TimeUnit.SECONDS) .assertFailure(IllegalStateException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void asyncMaxConcurrency() { for (int itemCount = 1; itemCount <= 100000; itemCount *= 10) { for (int concurrency = 1; concurrency <= 256; concurrency *= 2) { Flowable.range(1, itemCount) .flatMapCompletable( Functions.justFunction(Completable.complete() .subscribeOn(Schedulers.computation())) , false, concurrency) .test() .withTag("itemCount=" + itemCount + ", concurrency=" + concurrency) .awaitDone(5, TimeUnit.SECONDS) .assertResult(); } } } }