如何在rxjava中压缩

时间:2016-05-18 03:44:29

标签: multithreading asynchronous rx-java

您好我是RXJava的新手,我正在努力研究我的第一个RxJava示例,如下所示

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("MyTableViewCell", forIndexPath: indexPath) as! MyTableViewCell

    return cell
}

我想打印Observable<String> myObservable = Observable.create( new Observable.OnSubscribe<String>() { public void call(Subscriber<? super String> subscriber) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } subscriber.onNext("hello world"); subscriber.onCompleted(); } } ).subscribeOn(Schedulers.computation()); Observable<String> myObservable1 = Observable.just("thank you"); Observable observable3 = Observable.zip(myObservable, myObservable1, new Func2<String,String,String>() { @Override public String call(String o, String o2) { return o+":"+o2; } }); observable3.subscribe(new Action1<String>() { @Override public void call(String o) { System.out.println("before"); System.out.println(o); } }); System.out.println("after"); 然后before,如何使用rxjava并且不使用after

1 个答案:

答案 0 :(得分:1)

一种可能的方式:

 Observable<String> myObservable = Observable.create(
            new Observable.OnSubscribe<String>() {
                public void call(Subscriber<? super String> subscriber) {
                    subscriber.onNext("hello world");
                    subscriber.onCompleted();
                }
            }
    ).subscribeOn(Schedulers.computation());

    Observable<String> myObservable1 = Observable.just("thank you");

    Observable observable3 = Observable.zip(myObservable, myObservable1, new Func2<String,String,String>() {
        @Override
        public String call(String o, String o2) {
            return o+":"+o2;
        }
    });

    Observable.just("before").concatWith(observable3).concatWith(Observable.just("after")).toBlocking().subscribe(new Action1<String>() {
        @Override
        public void call(String s) {
            System.out.println(s);
        }
    });

结果是:

之前
你好世界:谢谢你 之后