1

Unfortunately, my REST Delete operation work only for one item. So what I was trying to do is,

Observable.just(items).flatMapIterable { items -> items }.flatMap {
                //call REST DELETE for every item
            }.flatMap {
                // call REST GET
            }

The problem is the GET call is being called for every item. How can I wait for finishing all the delete done and then perform the GET call?

Thanks in Advance.

RKRK
  • 1,284
  • 5
  • 14
  • 18
sadat
  • 4,004
  • 2
  • 29
  • 49
  • "How can I wait for finishing all the delete done" - it depends on what library is used to call delete operation. – Alexei Kaigorodov Aug 08 '19 at 03:05
  • What's the return type of REST DELETE call? – Bach Vu Aug 08 '19 at 03:12
  • I am not sure why that is important. Lets say, I just want to print each item and then print a string "Done". @AlexeiKaigorodov – sadat Aug 08 '19 at 03:12
  • @BachVu its void. – sadat Aug 08 '19 at 03:14
  • it is important. If you use an async library and call to rest api returns CompletableFuture, then to wait for all async calls, just invoke CompletableFuture.all(). Other async library may have similar facility. If you use sync library, then you need to convert sync calls to async calls using FixedThreadPool. – Alexei Kaigorodov Aug 08 '19 at 14:55

3 Answers3

1

In your case, you can apply toList() like this

fun doTask(items: List<String>):Observable<Boolean>{
        return Observable.fromIterable(items)
                .flatMap { processItem(it) }
                .toList()
                .toObservable()
                .flatMap { finalTask() }
    }
Bach Vu
  • 2,298
  • 1
  • 15
  • 19
  • Thanks @Bach Vu, I still could not solve the problem because of the return type. I did this fun doTask(items: ArrayList):Observable{ return Observable.fromIterable(items) .map { processItem(it) }.doOnComplete { finalTask() } } fun processItem(s: String): Observable { return Observable.just(s) } fun finalTask(): Observable{ return Observable.fromCallable { true } } //it has error in do Task method – sadat Aug 08 '19 at 03:55
  • @sadat Ok, because you said it returns void so I made it like that, check my editted answer :) – Bach Vu Aug 08 '19 at 04:39
0

The problem can be solved with zip. In case any wants this

fun doTask(items: ArrayList<String>): Observable<Boolean> {
    val list = arrayListOf<Observable<String>>()
    items.forEach {
        list.add(processItem(it))
    }
    return Observable.zip(list) {
    }.flatMap {
        finalTask()
    }
}

fun processItem(s: String): Observable<String> {
    print(s)
    return Observable.just(s.toUpperCase())
}

fun finalTask(): Observable<Boolean> {
    print("final")
    return Observable.fromCallable { true }
}
sadat
  • 4,004
  • 2
  • 29
  • 49
0
Observable.just("one", "two", "three", "four", "five").subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Log.d("ffff", s);//print: one, two, three, four, five
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        }, new Action() {
            @Override
            public void run() throws Exception {
                Log.d("ffff", "complete");//print: complete
            }
        });
taha
  • 731
  • 2
  • 7
  • 18