RxJava(二)

2017-08-15  本文已影响0人  珞神

前言

正文

一、变换

RxJava 提供了对事件序列进行变换的支持,这是它的核心功能之一,也是大多数人说『RxJava 真是太好用了』的最大原因。所谓变换,就是将事件序列中的对象或整个序列进行加工处理,转换成不同的事件或事件序列。

1.1 先介绍下 FunX 系列的方法,因为要用到
imageimage
/**
 * Represents a function with one argument.
 * @param <T> the first argument type
 * @param <R> the result type
 */
public interface Func1<T, R> extends Function {
    R call(T t);
}
1.2 变换之 map() ,看例子
Student student1 = new Student("张三",20);
Student student2 = new Student("李四",30);
Student student3 = new Student("王五",40);
//// 输入类型 Student
Observable.just(student1,student2,student3)
        .map(new Func1<Student, String>() {
               @Override
                public String call(Student student) {// 参数类型 Student

                    String name = student.getName();
                    return name;//返回类型 String
                }
            })
            .subscribe(new Action1<String>() {//参数类型 String
                @Override
                public void call(String s) {
                    Log.i("map",s);
                }
            });

student 类如下:

public class Student {

    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
    
}

可以看到 Observable 中原来的参数是 Student 对象,而最后我们需要的是String类型的 name,这里使用了 map 来实现这一转换的过程。

1.3 变换之 flatMap()
public class Student {

    private String name;
    private int age;
    //课程
    private ArrayList<Couser> courseList;
    
    ...
}

课程类如下:

public class Couser {

    private String couserName;
    private int couserId;
    
    ...
}
        ArrayList<Couser> list = new ArrayList<>();
        list.add(new Couser("语文",1));
        list.add(new Couser("数学",2));
        list.add(new Couser("英语",3));
        Student student1 = new Student("张三",20,list);
ArrayList<Couser> list1 = new ArrayList<>();
        list1.add(new Couser("政治",4));
        list1.add(new Couser("历史",5));
        list1.add(new Couser("地理",6));
        Student student2 = new Student("张三",20,list1);
        
        
 Observable.just(student1,student2)
                .map(new Func1<Student, ArrayList<Couser>>() {
                    @Override
                    public ArrayList<Couser> call(Student student) {
                        return student.getCourseList();
                    }
                })
                .subscribe(new Action1<ArrayList<Couser>>() {
                    @Override
                    public void call(ArrayList<Couser> cousers) {
                        for (Couser couser : cousers) {
                            Log.i("map",couser.getCouserName());
                        }
                    }
                });

注意此处观察者 Action1 的参数类型为 ArrayList<Couser> cousers 。

        Observable.just(student1,student2)
                .flatMap(new Func1<Student, Observable<Couser>>() {
                    @Override
                    public Observable<Couser> call(Student student) {

                        return Observable.from(student.getCourseList());
                    }
                })
                .subscribe(new Action1<Couser>() {
                    @Override
                    public void call(Couser couser) {
                        Log.i("map",couser.getCouserName());
                    }
                });

从上面的代码可以看出, flatMap() 和 map() 有一个相同点:它也是把传入的参数转化之后返回另一个对象。但需要注意,和 map() 不同的是, flatMap() 中返回的是个 Observable 对象

flatMap() 的原理

flatMap() 的优点

由于可以在嵌套的 Observable 中添加异步代码, flatMap() 也常用于嵌套的异步操作,例如嵌套的网络请求。这样可以把嵌套的请求写在一条链中,从而保持程序逻辑的清晰。也就是说在 return Observable.from(student.getCourseList()); 中做进一步的网络请求封装。

1.4 变换还有其他的方式,比如 lift 和 compose ,自行研究。。。

二、防抖动效果 throttleFirst()

//设置防抖间隔为 500ms
throttleFirst(500, TimeUnit.MILLISECONDS)

  Observable.just(student1,student2)
                .flatMap(new Func1<Student, Observable<Couser>>() {
                    @Override
                    public Observable<Couser> call(Student student) {

                        return Observable.from(student.getCourseList());
                    }
                })
                //防抖动
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                
                .subscribe(new Action1<Couser>() {
                    @Override
                    public void call(Couser couser) {
                        Log.i("map",couser.getCouserName());
                    }
                });
上一篇 下一篇

猜你喜欢

热点阅读