Skip to content

Commit

Permalink
为BaseSubscriber的onNext进行try-catch,防止在onNext中出现崩溃
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexiangjys committed Jun 15, 2018
1 parent fa61054 commit bf2b700
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ dependencies {
//rxbinding的sdk
implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'

// implementation project(':rxutil2')
implementation 'com.github.xuexiangjys:RxUtil2:1.1.2'
implementation project(':rxutil2')
// implementation 'com.github.xuexiangjys:RxUtil2:1.1.2'

//leak
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
* RxJavaUtils演示示例
Expand Down Expand Up @@ -93,16 +94,16 @@ public void doInUIThread(String s) {
});
break;
case R.id.btn_do_in_io_ui:
// RxJavaUtils.executeAsyncTask("我是入参789", new Function<String, Integer>() {
// RxJavaUtils.executeAsyncTask2("我是入参789", new Function<String, Integer>() {
// @Override
// public Integer apply(String s) throws Exception {
// Log.e(TAG, "[doInIOThread] " + getLooperStatus() + ", 入参:" + s);
// return 12345;
// }
// }, new Consumer<Integer>() {
// }, new SimpleSubscriber<Integer>() {
// @Override
// public void accept(Integer integer) throws Exception {
// Log.e(TAG, "[doInUIThread] " + getLooperStatus() + ", 入参:" + integer);
// public void onSuccess(Integer integer) {
// int a = 100/0;
// }
// });
RxJavaUtils.executeAsyncTask(new RxAsyncTask<String, Integer>("我是入参789") {
Expand All @@ -121,7 +122,7 @@ public void doInUIThread(Integer integer) {
case R.id.btn_loading:
RxJavaUtils.delay("加载完毕!", 3, TimeUnit.SECONDS, new ProgressLoadingSubscriber<String>(mProgressLoader) {
@Override
public void onNext(String s) {
public void onSuccess(String s) {
Toast.makeText(RxJavaActivity.this, s, Toast.LENGTH_SHORT).show();
}
});
Expand All @@ -145,7 +146,7 @@ public void onStart() {
}

@Override
public void onNext(Long aLong) {
public void onSuccess(Long aLong) {
mBtnCountDown.setText(String.format("%s s后重新获取", aLong));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public void onComplete() {
RxLog.e("-->Subscriber is Complete");
}

@Override
public void onNext(T t) {
try {
onSuccess(t);
} catch (Throwable e) {
e.printStackTrace();
onError(e);
}
}

@Override
public final void onError(Throwable e) {
Expand All @@ -67,8 +76,16 @@ public final void onError(Throwable e) {

/**
* 出错
*
* @param e
*/
public abstract void onError(RxException e);

/**
* 安全版的{@link #onNext},自动做了try-catch
*
* @param t
*/
public abstract void onSuccess(T t);

}

0 comments on commit bf2b700

Please sign in to comment.