Zhihu read a link, also gave me a lesson. Think of hongyang, Ren Yugang, Guo Lin and other predecessors are really not simple. At least as a small and medium-sized enterprise, their sharing is of great help to us and also drives our technology to a certain extent. After reading the link, I realized that the four words “don’t forget why you started” really mean a lot.

This week there are still a lot of people respond to this RxJava source analysis do not understand, I try to spend more time to do more thinking, here or for the source code to make up an article, do a source analysis again.

public class ObservableMap<T,R> extends Observable<R> {
    final Observable<T> source; Observable final Function<T, R>function; Public ObservableMap(ObservableMap <T>)source, Function<T, R> function) {
        this.source = source;
        this.function = function; } Override protected void subscribeActual(Observer<R> Observer) { Subscribe (new MapObserver(observer,function));
    }

    private class MapObserver<T> implements Observer<T>{
        final Observer<R> observer;
        final Function<T, R> function;
        public MapObserver(Observer<R> source, Function<T, R> function) {
            this.observer = source;
            this.function = function;
        }

        @Override
        public void onSubscribe() { observer.onSubscribe(); } @Override public void onNext(@NonNull T item) { // 4. Function. Apply try {R applyR = function. Apply (item); OnNext Observer. onNext(applyR); } catch (Exception e) { e.printStackTrace(); observer.onError(e); } } @Override public void onError(@NonNull Throwable e) { observer.onError(e); } @Override public voidonComplete() { observer.onComplete(); }}}Copy the code
Observable.just("http://img.taopic.com/uploads/allimg/130331/240460-13033106243430.jpg") .map(new Function<String, Bitmap>() {@override public Bitmap apply(String urlPath) throws Exception {// HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream inputStream = urlConnection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream);return bitmap;
                    }
                })
                .map(new Function<Bitmap, Bitmap>() {
                    @Override
                    public Bitmap apply(@NonNull Bitmap bitmap) throws Exception {
                        bitmap = createWatermark(bitmap, "RxJava2.0");
                        return bitmap;
                    }
                })
                .map(new Function<Bitmap, Bitmap>() {
                    @Override
                    public Bitmap apply(Bitmap bitmap) throws Exception {
                        returnbitmap; Subscribe (new Consumer<Bitmap>() {@override public void onNext(final Bitmap) {// step 7 mImage.setImageBitmap(bitmap); }});Copy the code

enlightenment

The map operator is a very simple operator. Once we understand this operator, we can basically understand the whole idea of RxJava source code, which means that you also know where to start analyzing other operators.

Source.subscribe (new MapObserver(observer,function)); We’re actually calling an upstream subscribe method, or we could say we’re recursively calling a method upstream, but Obsever has a static proxy object called MapObserver wrapped around it.

Finally, the upstream Observable calls the onNext method, which recurses downstream to the onNext method in the wrapped MapObserver. Here we see the function.apply(item) conversion process. And then you pass it down, and you end up with Consumer’s onNext method.

We can summarize a lot of information from the map operator, such as the fact that each operator must call the subscribe method upstream, and that the original Observer is usually static proxy wrapped, which leads to the onNext() method of our proxy object. So each operator only handles upstream and downstream. And we look at the source of other operators, it should be enough to analyze only two methods, one is the subscribeActual() method, one is the xxxObserver proxy object onNext method. We only need to really understand the source code of one operator, and then we have the whole idea of RxJava source code.

I remember that Fu Xi, the originator of Zhouyi, had only eight kinds of hexagrams. After The rearrangement of 64 kinds of hexagrams by King Wen of Zhou dynasty, zhouyi was created. Later Zhuge Liang, Li Chunfeng, Shao Kangjie, Liu Bowen all formed their own school. There is no mobile phone to play with, no live video to study, but the knowledge of astronomy and geography, the key point is very people are still indifferent to the vision.

RxJava source code analysis here, overall can still learn a lot of things, but also can review some previous knowledge of design patterns, but also can roughly understand the idea of responsive programming. I don’t know how many questions you have to ask, but many interviewers will say something like, “What did you read about the Android Sdk source code? How about responsive programming like RxJava source code?”

All shared outline: Android Advanced Journey – System Architecture chapter

Video on address: https://pan.baidu.com/s/1jIck0sQ