johnny_crq
johnny_crq

Reputation: 4391

RxJava Observable based on items on a list

I need an Observable that never ends, and just process some data and chain another observable when there are items on a list. Is there any way of accomplish that, and what would be the best approach=?

My closest idea was to create a timer observable and check every x seconds if there are items on the list. This idea is not ideal, because i need to process the data as soon as there are values on that list, which i modify outside the observable chain.

return Observable.timer(2, TimeUnit.SECONDS)
                .flatMap(integer -> captureList.getLatestCaptureCut())
                .flatMap(vp::processVideo)
                .observeOn(AndroidSchedulers.mainThread())
                .repeat()

Upvotes: 0

Views: 198

Answers (2)

Ivan Morgillo
Ivan Morgillo

Reputation: 3844

I would suggest a PublishSubject in your CaptureList class. Instead of providing a pull method getLatestCaptureCut(), you could provide a push method, with a Subject:

PublishSubject<VP> captured = PublishSubject.create();

You could then .subscribe() to the PublishSubject and process the data when they come in.

In your CaptureList you would call

captured.onNext(vp);

every time new data is available. For instance, in your setLatestCaptureCut(). I'm assuming you already have some kind of routine that generates the CaptureCut and store it, to make it available in getLatestCaptureCut().

Upvotes: 1

marwinXXII
marwinXXII

Reputation: 1446

I think you can use Subject, and push your next items there.

PublishSubject<Integer> subject = PublishSubject.<Integer>create();

subject.flatMap(integer -> captureList.getLatestCaptureCut())
    .flatMap(vp::processVideo)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe();

//push new items
subject.onNext(0);
subject.onNext(1);

Upvotes: 1

Related Questions