本文整理了Java中io.reactivex.Observable.as()
方法的一些代码示例,展示了Observable.as()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.as()
方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:as
[英]Calls the specified converter function during assembly time and returns its resulting value.
This allows fluent conversion to any other type. Scheduler: as does not operate by default on a particular Scheduler.
[中]
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void asNull() {
just1.as(null);
}
代码示例来源:origin: ReactiveX/RxJava
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void observableGenericsSignatureTest() {
A<String, Integer> a = new A<String, Integer>() { };
Observable.just(a).as((ObservableConverter)ConverterTest.testObservableConverterCreator());
}
代码示例来源:origin: bluelinelabs/Conductor
@NonNull
@Override
protected View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
Log.i(TAG, "onCreateView() called");
View view = inflater.inflate(R.layout.controller_lifecycle, container, false);
view.setBackgroundColor(ContextCompat.getColor(container.getContext(), R.color.purple_300));
unbinder = ButterKnife.bind(this, view);
tvTitle.setText(getResources().getString(R.string.rxlifecycle_title, TAG));
Observable.interval(1, TimeUnit.SECONDS)
.doOnDispose(new Action() {
@Override
public void run() {
Log.i(TAG, "Disposing from onCreateView()");
}
})
.as(AutoDispose.<Long>autoDisposable((scopeProvider)))
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long num) {
Log.i(TAG, "Started in onCreateView(), running until onDestroyView(): " + num);
}
});
return view;
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void as() {
Observable.just(1).as(new ObservableConverter<Integer, Flowable<Integer>>() {
@Override
public Flowable<Integer> apply(Observable<Integer> v) {
return v.toFlowable(BackpressureStrategy.MISSING);
}
})
.test()
.assertResult(1);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testAsExtend() {
final TestObserver<Object> to = new TestObserver<Object>();
final Object value = new Object();
Object returned = Observable.just(value).as(new ObservableConverter<Object, Object>() {
@Override
public Object apply(Observable<Object> onSubscribe) {
onSubscribe.subscribe(to);
to.assertNoErrors();
to.assertComplete();
to.assertValue(value);
return to.values().get(0);
}
});
assertSame(returned, value);
}
代码示例来源:origin: iMeiji/Toutiao
private void getSearchSuggest(String keyWord) {
RetrofitFactory.getRetrofit().create(IMobileSearchApi.class)
.getSearchSuggestion(keyWord.trim())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.as(this.bindAutoDispose())
.subscribe(bean -> suggestionAdapter.updateDataSource(bean.getData()), ErrorAction.error());
}
代码示例来源:origin: bluelinelabs/Conductor
public AutodisposeController() {
Observable.interval(1, TimeUnit.SECONDS)
.doOnDispose(new Action() {
@Override
public void run() {
Log.i(TAG, "Disposing from constructor");
}
})
.as(AutoDispose.<Long>autoDisposable((scopeProvider)))
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long num) {
Log.i(TAG, "Started in constructor, running until onDestroy(): " + num);
}
});
}
代码示例来源:origin: iMeiji/Toutiao
private void getSearchHistory() {
Observable
.create((ObservableOnSubscribe<List<SearchHistoryBean>>) e -> {
List<SearchHistoryBean> list = dao.queryAll();
e.onNext(list);
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.as(this.bindAutoDispose())
.subscribe(list -> historyAdapter.updateDataSource(list), ErrorAction.error());
}
代码示例来源:origin: iMeiji/Toutiao
private void setAdapter() {
Observable
.create((ObservableOnSubscribe<List<MediaChannelBean>>) e -> {
list = dao.queryAll();
e.onNext(list);
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider
.from(this, Lifecycle.Event.ON_DESTROY)))
.subscribe(list -> {
adapter.setItems(list);
adapter.notifyDataSetChanged();
if (list.size() == 0) {
tv_desc.setVisibility(View.VISIBLE);
} else {
tv_desc.setVisibility(View.GONE);
}
});
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void observableConverterThrows() {
try {
Observable.just(1).as(new ObservableConverter<Integer, Integer>() {
@Override
public Integer apply(Observable<Integer> v) {
throw new TestException("Forced failure");
}
});
fail("Should have thrown!");
} catch (TestException ex) {
assertEquals("Forced failure", ex.getMessage());
}
}
代码示例来源:origin: iMeiji/Toutiao
@Override
public void doLoadData(String qid) {
this.qid = qid;
Log.d(TAG, "doLoadArticle: " + qid);
RetrofitFactory.getRetrofit().create(IMobileWendaApi.class).getWendaNiceContent(qid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.as(view.bindAutoDispose())
.subscribe(wendaContentBean -> {
doSetHeader(wendaContentBean.getQuestion());
doSetAdapter(wendaContentBean.getAns_list());
niceOffset += 10;
}, throwable -> {
doShowNetError();
ErrorAction.print(throwable);
});
}
代码示例来源:origin: bluelinelabs/Conductor
@Override
protected void onAttach(@NonNull View view) {
super.onAttach(view);
Log.i(TAG, "onAttach() called");
(((ActionBarProvider) getActivity()).getSupportActionBar()).setTitle("Autodispose Demo");
Observable.interval(1, TimeUnit.SECONDS)
.doOnDispose(new Action() {
@Override
public void run() {
Log.i(TAG, "Disposing from onAttach()");
}
})
.as(AutoDispose.<Long>autoDisposable((scopeProvider)))
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long num) {
Log.i(TAG, "Started in onAttach(), running until onDetach(): " + num);
}
});
}
代码示例来源:origin: iMeiji/Toutiao
@Override
public void doLoadData(String url) {
RetrofitFactory.getRetrofit().create(IMobileWendaApi.class)
.getWendaAnsDetail(url)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.as(view.bindAutoDispose())
.subscribe(responseBody -> {
String result = getHTML(responseBody.string());
if (result != null) {
view.onSetWebView(result, true);
} else {
view.onSetWebView(null, false);
}
view.onHideLoading();
}, throwable -> {
view.onSetWebView(null, false);
view.onHideLoading();
ErrorAction.print(throwable);
});
}
代码示例来源:origin: iMeiji/Toutiao
@Override
public void doSaveImage() {
Observable
.create((ObservableOnSubscribe<Boolean>) e -> {
List<PhotoGalleryBean.SubImagesBean> sub_images = bean.getSub_images();
final String url = sub_images.get(position).getUrl();
Log.d(TAG, "doSaveImage: " + url);
e.onNext(DownloadUtil.saveImage(url, InitApp.AppContext));
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.as(view.bindAutoDispose())
.subscribe(aBoolean -> {
if (aBoolean) {
view.onShowSaveSuccess();
} else {
view.onShowNetError();
}
}, throwable -> {
view.onShowNetError();
ErrorAction.print(throwable);
});
}
代码示例来源:origin: uber/AutoDispose
@Test public void autoDispose_withSuperClassGenerics_compilesFine() {
Observable.just(new BClass())
.as(autoDisposable(ScopeProvider.UNBOUND))
.subscribe((Consumer<AClass>) aClass -> {
});
}
代码示例来源:origin: uber/AutoDispose
@Test public void verifyObserverDelegate() {
final AtomicReference<Observer> atomicObserver = new AtomicReference<>();
final AtomicReference<Observer> atomicAutoDisposingObserver = new AtomicReference<>();
try {
RxJavaPlugins.setOnObservableSubscribe((source, observer) -> {
if (atomicObserver.get() == null) {
atomicObserver.set(observer);
} else if (atomicAutoDisposingObserver.get() == null) {
atomicAutoDisposingObserver.set(observer);
RxJavaPlugins.setOnObservableSubscribe(null);
}
return observer;
});
Observable.just(1)
.as(autoDisposable(ScopeProvider.UNBOUND))
.subscribe();
assertThat(atomicAutoDisposingObserver.get()).isNotNull();
assertThat(atomicAutoDisposingObserver.get()).isInstanceOf(AutoDisposingObserver.class);
assertThat(((AutoDisposingObserver) atomicAutoDisposingObserver.get()).delegateObserver()).isNotNull();
assertThat(((AutoDisposingObserver) atomicAutoDisposingObserver.get()).delegateObserver()).isSameAs(
atomicObserver.get());
} finally {
RxJavaPlugins.reset();
}
}
代码示例来源:origin: uber/AutoDispose
@Test public void autoDispose_withProvider_withoutStartingLifecycle_shouldFail() {
BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER);
LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle);
Observable.just(1)
.as(autoDisposable(provider))
.subscribe(o);
o.takeSubscribe();
assertThat(o.takeError()).isInstanceOf(LifecycleNotStartedException.class);
}
代码示例来源:origin: uber/AutoDispose
@Test public void verifyCancellation() {
final AtomicInteger i = new AtomicInteger();
//noinspection unchecked because Java
final ObservableEmitter<Integer>[] emitter = new ObservableEmitter[1];
Observable<Integer> source = Observable.create(e -> {
e.setCancellable(i::incrementAndGet);
emitter[0] = e;
});
CompletableSubject scope = CompletableSubject.create();
source.as(autoDisposable(scope))
.subscribe();
assertThat(i.get()).isEqualTo(0);
assertThat(scope.hasObservers()).isTrue();
emitter[0].onNext(1);
scope.onComplete();
emitter[0].onNext(2);
// Verify cancellation was called
assertThat(i.get()).isEqualTo(1);
assertThat(scope.hasObservers()).isFalse();
}
代码示例来源:origin: uber/AutoDispose
@Test public void autoDispose_withProvider_afterLifecycle_shouldFail() {
BehaviorSubject<Integer> lifecycle = BehaviorSubject.createDefault(0);
lifecycle.onNext(1);
lifecycle.onNext(2);
lifecycle.onNext(3);
RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER);
LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle);
Observable.just(1)
.as(autoDisposable(provider))
.subscribe(o);
o.takeSubscribe();
assertThat(o.takeError()).isInstanceOf(LifecycleEndedException.class);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void compositeTest() {
CompositeConverter converter = new CompositeConverter();
Flowable.just(1)
.as(converter)
.test()
.assertValue(1);
Observable.just(1)
.as(converter)
.test()
.assertValue(1);
Maybe.just(1)
.as(converter)
.test()
.assertValue(1);
Single.just(1)
.as(converter)
.test()
.assertValue(1);
Completable.complete()
.as(converter)
.test()
.assertComplete();
Flowable.just(1)
.parallel()
.as(converter)
.test()
.assertValue(1);
}
内容来源于网络,如有侵权,请联系作者删除!