ThrottleFirst clever use

Prevents the View from being clicked continuously, sending only the first signal within a specified time period

RxView.clicks(btThrottleFirst) .throttleFirst(3, TimeUnit.SECONDS) .subscribe(new Action1() { @Override public void call(Void aVoid) { Toast.makeText(mContext, "I am a button that will not be clicked twice in 3 seconds ", toast.length_short).show(); }});Copy the code

PublishSubject

Unlike normal Subject, subscription events are not triggered immediately at subscription time, but allow us to manually invoke onNext(),onError(), and onCompleted at any time.

As you can see, the biggest difference between a PublishSubject and a regular Subject is that it can subscribe to events and then manually invoke methods to trigger events at some point. What can be done with it?

For example: when one interface changes, the other interface responds. Here is a Demo of communication between two fragments

1. First define the public PublishSubject object externally

PublishSubject publishSubject = PublishSubject.create();
PublishSubjectTopFragment topFragment = new PublishSubjectTopFragment(publishSubject);
PublishSubjectBottomFragment bottom_Fragment = new PublishSubjectBottomFragment(publishSubject);
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl_top,topFragment, "top")
.replace(R.id.fl_bottom, bottom_Fragment, "bottom")
.commit();
}Copy the code

2. TopFragment is responsible for sending information

@OnClick(R.id.btn_send) void sendToBottom(){ String result = et_input.getText().toString().trim(); /** * Manually call the method at this time to trigger the event: * issue the message */ publishsubject.onNext (result); }Copy the code

3. After the notification is received in BottomFragment, a response is generated

/** * subscribe to events: * Upon receipt of the notification, Subscribe (new Action1() {@override public void call(String s) {tv_result.settext (" tv_result.settext ("); "+s); }});Copy the code

Buffer

buffer(count = 3):

All the Buffer operator does is cache the size specified by the data installation, and then emit the cached data as a collection.Copy the code

buffer(count = 2, skip = 3):

Skip parameter is used to specify that several data need to be skipped in launching a set each time. If count is specified as 2 and SKIP is specified as 3, a set containing two data will be launched every 3 data. If count== SKIP, it will be equivalent to buffer(count = 3).Copy the code

Scan

The Scan operator applies a function to a sequence of data and emits the result of the function as the first argument when the next data is applied to the function, somewhat like a recursive operation

Observable. Just (1,2,3,4,5). Scan (new Func2() {@override public Integer call(Integer Integer, Integer integer2) { return integer*integer2; } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1() { @Override public  void call(Integer integer) { tvResult.append(integer+"\n"); }});Copy the code

Distinct

The use of the Distinct operator is to remove duplicates, which is easy to understand, and all duplicates are filtered out.

Observable. Just (1,2,3,1,1,2,1,4,5). Subscribe (new Action1() {@override public void call(Integer Integer) { tvResult.append(integer+""); }});Copy the code

The DistinctUntilChanged operator is used to filter out consecutive duplicate data.

Observable.just(1, 2, 3, 3, 3, 1, 2, 3, 3).distinctUntilChanged();
Copy the code

Filter

Only data that meets the filtering criteria is returned

Observable. Just (1,2,3,4,5). Filter (new Func1() {@override public Boolean call(Integer Integer) {return Integer >=3; }}). Subscribe (new Action1() {@override public void call(Integer Integer) {tvresult.append () {subscribe(new Action1() {@override public void call(Integer Integer) {tvresult.append (); "+integer); }});Copy the code

The Skip, Take

The Skip operator filters out the first n terms of the data emitted by the source Observable

The Take operator takes only the first n terms, which is easy to understand and use, but very useful. In addition to

The SkipLast and TakeLast operators filter from behind, respectively.

Observable. Just (" I "," I "," I "," I "," I "," I ").subscribe(2).subscribe(new Action1() {@override public void call(String s) { tvResult.append(s); }});Copy the code

Publish

What is a Connectable Observable?

It is a special Observable object that does not send data when Subscrib is Subscrib, but starts to send data only when connect operator is applied to it, so it can be used to control the timing of data transmission more flexibly.Copy the code

The Publish operator converts a normal Observable to a

A Connectable observables. Note that if the launch data has already started

Subscribe again only to receive data sent later

Connect

The Connect operator is used to trigger the Connectable Observable to emit data.Copy the code

The Connect operator returns a Subscription object, which terminates data transmission by calling its unsubscribe method.

In addition, applying the Connect operator when no subscriber has subscribed can cause it to start transmitting data.

use

Create a Connectable Observable using the Publish operator:

private ConnectableObservable publishObserver() {
Observable obser = Observable.interval(1, TimeUnit.SECONDS);
obser.observeOn(Schedulers.newThread());
    return obser.publish();
}Copy the code

Then create two Action1 objects and subscribe to them at different times:

ConnectableObservable obs = publishObserver();
Action1 action2 = o -> log("action2:" + o);
Action1 action1 = o -> {
log("action1:" + o);
    if ((long) o == 3) obs.subscribe(action2);
};
obs.subscribe(action1);

mLButton.setText("start");
mLButton.setOnClickListener(e -> mSubscription = obs.connect());
mRButton.setText("stop");
mRButton.setOnClickListener(e -> {
if (mSubscription != null) {
mSubscription.unsubscribe();
}
});Copy the code

Apply the Connect operator to the Connectable Observable when the start button is clicked and it starts emitting data.

Send action2 to the subscription when launching to 3, and both subscribers will receive the same data at the same time. Terminates its data transmission when the Stop button is clicked.

RefCount

The RefCount operator converts a Connectable Observable back into a normal Observable.

Say something

Oh, my God. Before you know it, it’s over. My god, a month, a month ago I planned to learn these things, and now, unexpectedly found in the end, it seems not a lot, and I feel quite satisfied, so angry ah.

Itself like to learn some fun things, so, usually not very busy, like to silently step on the pit, slowly fill up.

These operators are mainly too simple, so most of the comments and points I have written in the code, Demo here, interested can… Forehead, shy of say once, have interest of can ramble, in case have what appropriate, fate this kind of thing is hard to say ~~ github.com/GitHuborz/R…

Summary of monthly study plan

For a while, it was a little over.

One month before, prepare your learning plan for the month, start learning new things, RxJava, Retrofit, etc. From start listening to feel bad things, from the start Google search data, then slowly a little read, take notes and write Demo, feelings that some mainstream framework a powerful open source library and charm, although now the chance that the APP is not much, but about these new technologies or update from time to time, to learn more, or great.

Then, this week is mainly a systematic study of Rx I think practical, but also common operators, learn a, write a Demo test to keep up with, plus their own understanding, basic I think these are enough for daily use, as for not enough, you can also customize the operator, right? O ~ O (studying studying)

This period is time to learn, also sorted out a large part of the notes, ready to sort out, a small update C blog (RXJava (series), haven’t moved for a long time ~ there are cloud notes, can not help, lazy.

The main content of this study, have recorded notes, have also written the corresponding Demo, basically these: 2. RetrofitRxJavaDemo (a small demo that uses RxJava + Retrofit)** github.com/GitHuborz/M…

An RxJava+Retrofit+Mvp+… Browse Gank. Sister’s learning small Demo github.com/GitHuborz/M…

RxJava + use Demo github.com/GitHuborz/R…

Shy (✿ ◡ ‿ ◡) ~