缩短篇幅,直入主题:
RxJava 主要的作用就是链式完成异步操作,并且非常强大.
在Android项目中添加
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
写法:
//1:定义一个 可以被观察的对象
Observable observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext("Hello");
subscriber.onNext("Hi");
subscriber.onCompleted();
}
});
//等效以上写法
Observable observable1 = Observable.just("Hello", "Hi", "Aloha");
//等效以上写法
String[] words = {"Hello", "Hi", "Aloha"};
Observable observable2 = Observable.from(words);
//2:将可以被观察的对象,交给观察者执行
observable.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
L.e("onCompleted");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
L.e("onNext -- " + s);
}
});
基本-使用示例:
Observable.just("Hello").subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.e("RSenL", "call -- " + s);//将在调用者线程执行
}
});
Observable.just("Hello")
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.e("RSenL", "call -- " + s);//将在主线程执行
}
});
Observable.just("Hello")
.observeOn(Schedulers.newThread())
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.e("RSenL", "call -- " + s);//将在新线程执行
}
});
前台调用-后台执行 写法:
Observable.just("Hello")
.doOnSubscribe(new Action0() {
@Override
public void call() {
Log.e("RSenL", "call -- On Main Thread" );//主线程
}
})
.subscribeOn(AndroidSchedulers.mainThread())//必须此行,顺序必须在后
.observeOn(Schedulers.newThread())
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.e("RSenL", "call -- " + s);//新线程
}
});
后台调用-前台执行 写法:
Observable.just("Up Image").doOnSubscribe(new Action0() {
@Override
public void call() {
try {
Log.e("1", "tid:" + Thread.currentThread().getId());//io线程
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())//必须在后面
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
try {
Log.e("2", "tid:" + Thread.currentThread().getId());//主线程
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
//或者 ---------------------
Observable.just("Hello")
.map(new Func1<String, String>() {
@Override
public String call(String s) {
Log.e("RSenL", "thread id:" + Thread.currentThread().getId());//新线程
return null;
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.e("RSenL", "thread id:" + Thread.currentThread().getId());//主线程
}
});
Rx 线程控制, 写法:
Observable.just("a", "b", "c").subscribeOn(Schedulers.io())//决定提交在io线程
.doOnSubscribe(new Action0() {
@Override
public void call() {
Log.e("0", "tid:" + Thread.currentThread().getId());//主线程
}
})
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.newThread()).map(new Func1<String, String>() {
@Override
public String call(String s) {
Log.e("1", "tid:" + Thread.currentThread().getId());//新线程
return null;
}
}).observeOn(AndroidSchedulers.mainThread()).map(new Func1<String, String>() {
@Override
public String call(String s) {
Log.e("2", "tid:" + Thread.currentThread().getId());//主线程
return null;
}
}).observeOn(Schedulers.newThread()).map(new Func1<String, String>() {
@Override
public String call(String s) {
Log.e("3", "tid:" + Thread.currentThread().getId());//新线程
return null;
}
}).filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
Log.e("4-filter", "tid:" + Thread.currentThread().getId());//新线程
return true;
}
}).take(4)
.observeOn(Schedulers.io()).map(new Func1<String, Object>() {
@Override
public Object call(String s) {
Log.e("4", "tid:" + Thread.currentThread().getId());//io线程
return null;
}
}).subscribe(new Action1<Object>() {
@Override
public void call(Object o) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.e("5", "tid:" + Thread.currentThread().getId());//io线程
}
});
RxJava的异常捕捉:
Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
p("-->OnSubscribe");
subscriber.onNext("next");
subscriber.onNext("next");
subscriber.onNext("next");
subscriber.onNext("next");
int i = 100/0;
subscriber.onCompleted();//必须放在最后,否则异常捕捉不到
}
}).subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
p("-->onCompleted");
}
@Override
public void onError(Throwable throwable) {
p("-->onError");
}
@Override
public void onNext(String s) {
p("-->onNext");
}
});
Observable.just("--").doOnCompleted(new Action0() {
@Override
public void call() {
bottomTip.setText("-->doOnCompleted");
p("-->doOnCompleted");
}
}).doOnError(new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
bottomTip.setText("-->doOnError");
p("-->doOnError");
}
}).doOnNext(new Action1<String>() {
@Override
public void call(String s) {
bottomTip.setText("-->doOnNext");
p("-->doOnNext");
// int i = 100/0;
}
}).subscribe(new Action1<String>() {
@Override
public void call(String s) {
bottomTip.setText("-->subscribe");
// int i = 100/0;
p("-->subscribe");
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
p("-->onError");
}
});
未完待续…
http://gank.io/post/560e15be2dca930e00da1083
http://segmentfault.com/a/1190000004160294
至此: 文章就结束了,如有疑问: QQ群:274306954 欢迎您的加入.