1

I'm new to android, so have some questions regarding api calls.

Currently i use Retrofit to accomplish my api calls.

Here is example of my api call with retrofit

@POST("posts/new")
fun createPost(@Body post: Post, @Header("Authorization") token: String): Single<PostResult>

So, assume i have 10 posts and i need to call createPost 10 times (Yes, i know i can have list input on BE side, but ... ). Best way is to iterate over posts (for/map) and send them to the server.

But here is problem:
- How do i can track that all calls are done?

In JS i can have something like Promise.all - could i do something similar in android?

I thought about counting the finished vs started requests, but i think it's bit ugly isn't?

Андрей Гузюк
  • 2,134
  • 6
  • 29
  • 53

2 Answers2

1

in your response success you need to call again get/post method. after success you know your api call is done or else it throw error.

0

Thanks to @shkschneider

Your question is broad. You could use RxJava to zip, or coroutines to async/await or other methods.

Ended up with usage of .zip

Single.zip(observables) { args -> args }
      .subscribeOn(Schedulers.io())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe({ ...Success }, { ...Failure })
Андрей Гузюк
  • 2,134
  • 6
  • 29
  • 53