How to stop Observable.fromiterable and restart iteration with new list?

169 views Asked by At

I am having a list of images which i wanted to download.So below is the code which i used:

Observable.fromIterable(imagelist).subscribeOn(Scheduler.io). subscribe {
//download logic
}.addtodisposible(compositedisposible)

Initially, all downloaded images are saved into folder A. Now I have written a condition inside this iteration. if that condition satisfies, it should break the loop and call the same function again with only remaining items that are left to be iterated/downloaded and store it in folder B. Note:-1-> I tried to use takeuntil and added a Boolean value which i turn true if that condition satisfies.but iteration doesn't stop. 2-> if i clear composite disposible, the iteration stops and iteration with new items also begins, but iterates only few items.

Please help. Thank you.

1

There are 1 answers

1
akarnokd On

Sounds like you need a phase change in your subscribe's onNext call since you have to process the full imagelist either way. Just have an external variable that remembers which phase are you in and do an if in the onNext call:

var phase = AtomicBoolean();

Observable.fromIterable(imagelist)
.subscribeOn(Scheduler.io)
.subscribe {
    if (!phase.get()) {
        // download to folder A
        // process the image here
        // then make a decision if it is time to switch
        if (someCondition) {
            phase.set(true)
        }
    } else {
        // download to folder B
    }
}.addtodisposible(compositedisposible)