您的位置:首页 > 编程语言 > Java开发

RxJava 初识

2015-12-24 16:24 465 查看
响应式编程越来越流行,我也不得不来跟随主流,主要和大家分享RxJaVA

RxJava 类似于java观察者模式,但是又有区别

RxJava 主要分为两大部分

发布者

订阅者

首先来一个实例

findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Observable<String> myObservable = Observable.create(
new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> sub) {
sub.onNext("Hello, world!");
sub.onCompleted();
}
}
);
myObservable.subscribe(mySubscriber);
}
});

mySubscriber = new Subscriber<String>() {
@Override
public void onStart() {//默认一定会执行
super.onStart();
LogUtils.d("----->onStart()");
}

@Override
public void onCompleted() {
LogUtils.d("----->onCompleted()");
}

@Override
public void onError(Throwable e) {
LogUtils.d("----->onError()" + e);
}

@Override
public void onNext(String o) {
LogUtils.d("----->onNext()" + o);
}
};


执行log:
12-24 08:22:30.871 32235-32235/com.xuan.rxjavatest1 D/---->: ----->onStart()

12-24 08:22:30.881 32235-32235/com.xuan.rxjavatest1 D/---->: ----->onNext()Hello, world!

12-24 08:22:30.881 32235-32235/com.xuan.rxjavatest1 D/---->: ----->onCompleted()

myObservable.subscribe(mySubscriber); 是将发布者和订阅者联系起来

如果只为这种简单的逻辑,我们可以简化为:

findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*       Observable<String> myObservable = Observable.create(
new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> sub) {
sub.onNext("Hello, world!");
sub.onCompleted();
}
}
);
myObservable.subscribe(mySubscriber);*/

Observable<String> myObservable = Observable.just("Hello, world!");
myObservable.subscribe(mySubscriber);
}

});

mySubscriber = new Subscriber<String>() {
@Override
public void onStart() {//默认一定会执行
super.onStart();
LogUtils.d("----->onStart()");
}

@Override
public void onCompleted() {
LogUtils.d("----->onCompleted()");
}

@Override
public void onError(Throwable e) {
LogUtils.d("----->onError()" + e);
}

@Override
public void onNext(String o) {
LogUtils.d("----->onNext()" + o);
}
};


得到的结果:
12-24 08:32:03.951 16158-16158/com.xuan.rxjavatest1 D/---->: ----->onStart()

12-24 08:32:03.951 16158-16158/com.xuan.rxjavatest1 D/---->: ----->onNext()Hello, world!

12-24 08:32:03.951 16158-16158/com.xuan.rxjavatest1 D/---->: ----->onCompleted()

如果我们不关心OnComplete和OnError,一直向下走,那么将又是一片天地

源码

/**
* Subscribes to an Observable and provides callbacks to handle the items it emits and any error
* notification it issues.
* <dl>
*  <dt><b>Scheduler:</b></dt>
*  <dd>{@code subscribe} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param onNext
*             the {@code Action1<T>} you have designed to accept emissions from the Observable
* @param onError
*             the {@code Action1<Throwable>} you have designed to accept any error notification from the
*             Observable
* @return a {@link Subscription} reference with which the {@link Observer} can stop receiving items before
*         the Observable has finished sending them
* @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a>
* @throws IllegalArgumentException
*             if {@code onNext} is null, or
*             if {@code onError} is null
*/
public final Subscription subscribe(final Action1<? super T> onNext, final Action1<Throwable> onError) {
if (onNext == null) {
throw new IllegalArgumentException("onNext can not be null");
}
if (onError == null) {
throw new IllegalArgumentException("onError can not be null");
}

return subscribe(new Subscriber<T>() {

@Override
public final void onCompleted() {
// do nothing
}

@Override
public final void onError(Throwable e) {
onError.call(e);
}

@Override
public final void onNext(T args) {
onNext.call(args);
}

});
}
这样的重载方法有很多:

如果只关心下一个

<pre name="code" class="java"><span style="font-style: italic;">  </span>  Observable.just("Hello, world!").subscribe(new Action1<String>() {
@Override
public void call(String s) {
LogUtils.d("----->s");
}
});
如果关心下一个和错误完成状态灯:[/code]
<span style="color: rgb(128, 128, 128); font-family: 宋体; font-size: 9pt;"><span style="font-family:Arial;color:#333333;"><span style="font-size: 14px;">
</span></span></span><pre name="code" class="java">Observable.just("Hello").subscribe(new Action1<String>() {
@Override
public void call(String s) {
LogUtils.d("----->call:onNext" + s);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
LogUtils.d("----->call:" + throwable);
}
}, new Action0() {
@Override
public void call() {
LogUtils.d("----->call:complete");
}
});



<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px;">打印的结果:</span>
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px;">12-24 08:48:12.121 11725-11725/com.xuan.rxjavatest1 D/---->: ----->call:onNextHello
12-24 08:48:12.121 11725-11725/com.xuan.rxjavatest1 D/---->: ----->call:complete
</span>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: