简体   繁体   中英

RxJava: Return an Observable after some delay

I'm new to RxJava and need some help to complete my task.

1.

I have a service that should paint the shapes with speed one per second and return an Observable with that shape. 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.

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. 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. intervale will emit each seconds (and will never complete if you don't terminate your stream)

 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

For the second question, you can look at the operator buffer : you can buffer items in a list of 5 elements for example

    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);
             });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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