RxJava 命令大全(一)

Create: 创建一个Observable 

 public static void  rxcreate() {
      Observable.create(new Observable.OnSubscribe<String>() {

        @Override
        public void call(Subscriber<? super String> t) {
            // TODO Auto-generated method stub
            if(!t.isUnsubscribed()) {
                for(int i=0;i<5;i++) {
                    t.onNext(i+"");
                }
                t.onCompleted();
            }
        }
      }).subscribe(new Subscriber<String>() {

        @Override
        public void onCompleted() {
            // TODO Auto-generated method stub
              System.out.println("onCompleted: ");
        }

        @Override
        public void onError(Throwable e) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void onNext(String t) {
            // TODO Auto-generated method stub
              System.out.println("Next: " + t);
        }
    });
  }

Defer: 直到订阅发生时才创建

      public static void  rxdefer() {
          Observable.defer(new Func0<Observable<String>>() {

            @Override
            public Observable<String> call() {
                // TODO Auto-generated method stub
                System.out.println("call");
                String[] s={"zhangshan","lisi"};
                return Observable.from(s);
            }
        }).delaySubscription(1000, TimeUnit.MILLISECONDS)
         . subscribe(System.out::println);
         try {
            Thread.currentThread().sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }

Empty:创建一个不发送内容但会正常结束的

NEVER:创建一个不发送内容但不会正常结束的

Error: 创建一个不发送内容但以错误终止的

    Observable.empty().subscribe(new Subscriber<Object>() {

            @Override
            public void onCompleted() {
                // TODO Auto-generated method stub
                  System.out.println("onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                // TODO Auto-generated method stub
                  System.out.println("onError: ");
            }

            @Override
            public void onNext(Object t) {
                // TODO Auto-generated method stub
                  System.out.println("onNext: "+t);
            }

        });
View Code
        Observable.error(new Throwable("error ")).subscribe(new Subscriber<Object>() {

            @Override
            public void onCompleted() {
                // TODO Auto-generated method stub
                  System.out.println("onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                // TODO Auto-generated method stub
                  System.out.println("onError: ");
            }

            @Override
            public void onNext(Object t) {
                // TODO Auto-generated method stub
                  System.out.println("onNext: "+t);
            }

        });
View Code
        Observable.never().subscribe(new Subscriber<Object>() {

            @Override
            public void onCompleted() {
                // TODO Auto-generated method stub
                  System.out.println("onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                // TODO Auto-generated method stub
                  System.out.println("onError: ");
            }

            @Override
            public void onNext(Object t) {
                // TODO Auto-generated method stub
                  System.out.println("onNext: "+t);
            }

        });
View Code

From:通过对象或数据结构创建

array iterate

public static void rxFrom() {
        String[] s = {"ame","dsklj","dee"};
        List<String> list = Arrays.asList(s);
        Observable.from(list).subscribe(new Subscriber<Object>() {

            @Override
            public void onCompleted() {
                // TODO Auto-generated method stub
                  System.out.println("onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                // TODO Auto-generated method stub
                  System.out.println("onError: ");
            }

            @Override
            public void onNext(Object t) {
                // TODO Auto-generated method stub
                  System.out.println("onNext: "+t);
            }

        });
    }
View Code

 

Future

    FutureTask<String>  ft=new FutureTask<>(cal);
        Observable.from(ft,Schedulers.io())
        .doOnSubscribe(()-> ft.run())
        .subscribeOn(Schedulers.io())
        .subscribe(new Subscriber<Object>() {

            @Override
            public void onCompleted() {
                // TODO Auto-generated method stub
                  System.out.println("onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                // TODO Auto-generated method stub
                  System.out.println("onError: ");
            }

            @Override
            public void onNext(Object t) {
                // TODO Auto-generated method stub
                  System.out.println("onNext: "+t);
            }
        });
try {
    Thread.sleep(3000);
} catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

Interval: 间隔一段时间发送

    }
    public static void rxInterval() {
        String[] s = {"ame","dsklj","dee"};
        Observable.interval(1000,TimeUnit.MILLISECONDS).take(5).subscribe(new Subscriber<Object>() {

            @Override
            public void onCompleted() {
                // TODO Auto-generated method stub
                  System.out.println("onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                // TODO Auto-generated method stub
                  System.out.println("onError: ");
            }

            @Override
            public void onNext(Object t) {
                // TODO Auto-generated method stub
                  System.out.println("onNext: "+t);
            }

        });
        try {
            Thread.currentThread().sleep(5000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
View Code

Just: 发布一个对象

public static void rxJust() {
        Observable.just(1, 2, 3)
        .subscribe(new Subscriber<Integer>() {
      @Override
      public void onNext(Integer item) {
          System.out.println("Next: " + item);
      }

      @Override
      public void onError(Throwable error) {
          System.err.println("Error: " + error.getMessage());
      }

      @Override
      public void onCompleted() {
          System.out.println("Sequence complete.");
      }
  });
    }
View Code

Range:发布一个整数区域

    Observable.range(0, 10)
        .subscribe(new Subscriber<Integer>() {
      @Override
      public void onNext(Integer item) {
          System.out.println("Next: " + item);
      }

      @Override
      public void onError(Throwable error) {
          System.err.println("Error: " + error.getMessage());
      }

      @Override
      public void onCompleted() {
          System.out.println("Sequence complete.");
      }
  });
View Code

Repeat: 重复次数

    Observable.range(0, 10).repeat(3)
        .subscribe(new Subscriber<Integer>() {
      @Override
      public void onNext(Integer item) {
          System.out.println("Next: " + item);
      }

      @Override
      public void onError(Throwable error) {
          System.err.println("Error: " + error.getMessage());
      }

      @Override
      public void onCompleted() {
          System.out.println("Sequence complete.");
      }
  });
View Code

Start:

    public static void rxStart() {
        Observable.fromCallable(new Callable<String>() {

            @Override
            public String call() throws Exception {
                // TODO Auto-generated method stub
                System.out.println("call me");
                return null;
            }
        }).subscribe(new Subscriber<Object>() {

            @Override
            public void onCompleted() {
                // TODO Auto-generated method stub
                  System.out.println("onCompleted: ");
            }

            @Override
            public void onError(Throwable e) {
                // TODO Auto-generated method stub
                  System.out.println("onError: ");
            }

            @Override
            public void onNext(Object t) {
                // TODO Auto-generated method stub
                  System.out.println("onNext: "+t);
            }
        });
try {
    Thread.sleep(3000);
} catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
    }
View Code

 

 

Timer: 延迟一段时间后发布

    public static void rxTimer() {
        
        long time1=System.currentTimeMillis();
        Observable.timer(2000, TimeUnit.MILLISECONDS)
        .subscribe(new Subscriber<Long>() {
      @Override
      public void onNext(Long item) {
          System.out.println("start ="+(System.currentTimeMillis()-time1));
          System.out.println("Next: " + item);
      }

      @Override
      public void onError(Throwable error) {
          System.err.println("Error: " + error.getMessage());
      }

      @Override
      public void onCompleted() {
          System.out.println("Sequence complete.");
      }
  });
        try {
            Thread.currentThread().sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
View Code

 

转载于:https://www.cnblogs.com/dikeboy/p/10074554.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值