Promise promise = new Promise();
promise.observerOn(Schedulers.io()).then(new PromiseAction() {
@Override
public void call() {
Log.i("src_test", " testMock 1 in :" + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).then(new PromiseFunction1<Void, String>() {
@Override
public String call(Void aVoid) {
return "1234";
}
}).observerOn(AndroidSchedulers.mainThread()).then(new PromiseAction1() {
@Override
public void call(Object o) {
Log.i("src_test", " testMock 2 in :" + Thread.currentThread().getName() + " " + i);
textView.setText(o + "mock finish :" + (i++));
}
});
- observerOn: 指定下一个then Action执行的线程
- then: 设置当前需要执行的promise Action
- PromiseAction 无参Action
- PromiseAction1 有参Action且类型为I
- PromiseFunction 无参转换Func,结果类型为O
- PromiseFunction1<I, O> 将I类型参数转为O类型
Promise promise = new Promise();
promise.observerOn(Schedulers.newThread()).then(new PromiseAction() {
@Override
public void call() {
Log.i("src_test", " testMock2 1 in :" + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).observerOn(AndroidSchedulers.mainThread()).then(new PromiseAction() {
@Override
public void call() {
Log.i("src_test", " testMock2 2 in :" + Thread.currentThread().getName() + " " + i);
textView2.setText("mock finish :" + (i++));
}
});
JugglePromise.getInstance(this).append(promise);