简体   繁体   English

RxJava:延迟一段时间后返回一个Observable

[英]RxJava: Return an Observable after some delay

I'm new to RxJava and need some help to complete my task. 我是RxJava的新手,需要一些帮助来完成我的任务。

1. 1。

I have a service that should paint the shapes with speed one per second and return an Observable with that shape. 我有一项服务,该服务应以每秒一的速度绘制形状,并返回具有该形状的Observable。 It means, method shoud return the shape after one second after request. 这意味着,方法应在请求后一秒钟后返回形状。 Without delay it works. 刻不容缓。 I was told to use timers, but have trouble with implementing. 我被告知要使用计时器,但是在实现时遇到了麻烦。

I tried to use something like that, but it doesn't return anything: 我试图使用类似的东西,但是它不返回任何东西:

public Observable<PaintedCircle> paint(Shape shape) {
        return Observable
                .timer(1, TimeUnit.SECONDS)
                .flatMap(x -> Observable.just(new PaintedCircle(shape.getSize())));
    }

When I do it without flatMap it returns the object immediately. 当我不使用flatMap它将立即返回该对象。

2. 2。

My producer produces a list with some amount of shape objects (circles and squares). 我的生产者生成了一个列表,其中包含一些形状对象(圆形和正方形)。 I need to throw away too little circles, paint shapes (service described in the first question) and then put the shapes into the "boxes" — 5 pieces into each box in the order they returned from painting. 我需要扔掉太小的圆圈,绘制形状(第一个问题中所述的服务),然后将形状放入“盒子”中-按照从绘画中返回的顺序,将每个盒子中的5件放入盒子中。 Then all shapes from each box should be printed to console. 然后,应将每个盒子中的所有形状都打印到控制台上。

Question: can it be done in the common stream? 问题:可以在通用流中完成吗? How? 怎么样?

I started the stream like this, but need help to go on: 我像这样启动了视频流,但需要帮助才能继续:

Producer producer = new Producer();
        Observable.from(producer.produceShapes(20))
                .filter(shape -> shape instanceof Square || shape instanceof Circle && shape.getSize() > CIRCLE_MIN_SIZE)
                .flatMap(shape -> shape.getPaintingService().paint(shape));
//                .subscribe(System.out::print);
    }

I'm not shure to understand your first question, but if you want to emit a value each seconds, look at the interval operator instead : timer will emit once after the first second, then complete. 我不是必须理解您的第一个问题,但是如果您想每秒钟发出一个值,请查看interval运算符: timer将在第一秒之后发出一次,然后完成。 intervale will emit each seconds (and will never complete if you don't terminate your stream) intervale会每秒发射一次(如果不终止流,它将永远不会完成)

 public Observable<PaintedCircle> paint(Shape shape) {
    return Observable.interval(1, TimeUnit.SECONDS)  
                     .map(x -> new PaintedCircle(shape.getSize());
}

Please note : in this case, map can be used instead of flatMap 请注意:在这种情况下,可以使用map代替flatMap

For the second question, you can look at the operator buffer : you can buffer items in a list of 5 elements for example 对于第二个问题,您可以查看运算符buffer :例如,您可以在5个元素的列表中缓冲项目

    Producer producer = new Producer();
    Observable.from(producer.produceShapes(20))
              .filter(shape -> shape instanceof Square || shape instanceof Circle && shape.getSize() > CIRCLE_MIN_SIZE)
             .flatMap(shape -> shape.getPaintingService().paint(shape));
             .buffer(5)
             // shapes will be a list of 5 shape
             .map(shapes -> new Box(shapes))
             .subscribe(box -> {
                  System.out.println("BOX ---> " + box.getName());
                  box.getShapes().foreach(System.out::println);
             });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM