use of io.reactivex.functions.Action in project RxJava by ReactiveX.
the class MaybeFromActionTest method fromActionTwice.
@Test
public void fromActionTwice() {
final AtomicInteger atomicInteger = new AtomicInteger();
Action run = new Action() {
@Override
public void run() throws Exception {
atomicInteger.incrementAndGet();
}
};
Maybe.fromAction(run).test().assertResult();
assertEquals(1, atomicInteger.get());
Maybe.fromAction(run).test().assertResult();
assertEquals(2, atomicInteger.get());
}
use of io.reactivex.functions.Action in project RxJava by ReactiveX.
the class MaybeFromActionTest method noErrorLoss.
@Test
public void noErrorLoss() throws Exception {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final CountDownLatch cdl1 = new CountDownLatch(1);
final CountDownLatch cdl2 = new CountDownLatch(1);
TestObserver<Object> to = Maybe.fromAction(new Action() {
@Override
public void run() throws Exception {
cdl1.countDown();
cdl2.await();
}
}).subscribeOn(Schedulers.single()).test();
assertTrue(cdl1.await(5, TimeUnit.SECONDS));
to.cancel();
cdl2.countDown();
int timeout = 10;
while (timeout-- > 0 && errors.isEmpty()) {
Thread.sleep(100);
}
TestHelper.assertUndeliverable(errors, 0, InterruptedException.class);
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.functions.Action in project RxJava by ReactiveX.
the class MaybeFromActionTest method callable.
@SuppressWarnings("unchecked")
@Test
public void callable() throws Exception {
final int[] counter = { 0 };
Maybe<Void> m = Maybe.fromAction(new Action() {
@Override
public void run() throws Exception {
counter[0]++;
}
});
assertTrue(m.getClass().toString(), m instanceof Callable);
assertNull(((Callable<Void>) m).call());
assertEquals(1, counter[0]);
}
use of io.reactivex.functions.Action in project RxFacebook by YouClap.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
callbackManager = CallbackManager.Factory.create();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
List<String> perm = new ArrayList<>();
perm.add("email");
perm.add("public_profile");
RxFacebookLogin.logInWithReadPermissions(perm).subscribe(new Consumer<LoginResult>() {
@Override
public void accept(@NonNull LoginResult loginResult) throws Exception {
Log.d(LOG_TAG, "accept " + loginResult.getAccessToken());
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable) throws Exception {
Log.e(LOG_TAG, "error ", throwable);
}
}, new Action() {
@Override
public void run() throws Exception {
Log.e(LOG_TAG, "onCompleted");
}
});
}
});
}
use of io.reactivex.functions.Action in project Mvp-Rxjava-Retrofit-dagger2 by pengMaster.
the class RecordSuperviseListPresenter method downLoadSuperviseRecordBaseInfo.
/**
* 下载监督记录基本信息
*
* @param id
*/
public void downLoadSuperviseRecordBaseInfo(final String id) {
Observable<SuperviseContentGsonBean> superviseContent = mModel.getSuperviseContentNet(id);
Observable<superviseForm> recordBaseInfo = mModel.getRecordBaseInfo(id);
Observable<LawDocumentGsonBean> lawFileNet = mModel.getLawFileNet(id);
Observable.zip(recordBaseInfo, superviseContent, lawFileNet, new Function3<superviseForm, SuperviseContentGsonBean, LawDocumentGsonBean, RecordBaseGsonBean>() {
@Override
public RecordBaseGsonBean apply(@NonNull superviseForm superviseForm, @NonNull SuperviseContentGsonBean categoryResult, @NonNull LawDocumentGsonBean lawDocumentGsonBean) throws Exception {
// 合并两次请求的结果
RecordBaseGsonBean baseGsonBean = new RecordBaseGsonBean();
baseGsonBean.setSuperviseForm(superviseForm);
baseGsonBean.setCategoryResult(categoryResult);
baseGsonBean.setLawDocumentGsonBean(lawDocumentGsonBean);
baseGsonBean.setId(id);
return baseGsonBean;
}
}).subscribeOn(Schedulers.io()).doOnSubscribe(new // 在执行任务前,做准备操作
Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
// 在执行任务之前 do some thing ...
mRootView.showLoading();
}
}).observeOn(AndroidSchedulers.mainThread()).doFinally(new // 任务结束 do some thing ...
Action() {
@Override
public void run() throws Exception {
mRootView.hideLoading();
}
}).subscribe(new Consumer<RecordBaseGsonBean>() {
@Override
public void accept(@NonNull RecordBaseGsonBean recordBaseGsonBean) throws Exception {
mRootView.hideLoading();
mRootView.startActivity(recordBaseGsonBean);
}
}, new Consumer<Throwable>() {
@Override
public void accept(@NonNull Throwable throwable) throws Exception {
ToastUtils.showShort(throwable.getMessage());
}
});
}
Aggregations