inorichi
inorichi

Reputation: 406

Updating flatMap concurrent limit in the same subscription

I have an Android service that downloads files when a PublishSubject receives download events through EventBus and I want to limit the number of concurrent downloads based on a setting.

When the service is instantiated, it creates the PublishSubject and the following subscription:

PublishSubject<DownloadEvent> downloadsSubject = PublishSubject.create();

Subscription downloadSubscription = downloadsSubject
                .subscribeOn(Schedulers.io())
                .filter(event -> !isDownloaded(event))
                .flatMap(this::addDownloadToQueue)
                .flatMap(this::startDownload, preferences.getDownloadThreadsNumber())
                .onBackpressureBuffer()
                .subscribe();

But the setting is obtained only when the subscription is made, and changes to the setting have no effect.

Is there a way to update this value (or another approach) for next queue emissions without having to subscribe again?

Upvotes: 2

Views: 265

Answers (1)

akarnokd
akarnokd

Reputation: 69997

Here is a runnable class with a custom operator which should do what you wanted.

There are several race conditions in such scenarios and I've tried to cover most of them. The operator doesn't coordinate backpressure so you may need onBackpressureBuffer.

Upvotes: 1

Related Questions