Hello. I'm using asynchronous methods in my app; However, if there're 2 methods, f.ex
method1();
method2();
and both of them use Retrofit, they'll be executed both at once.
How to make method2() wait for method1() to complete first?
Even if there'is
method1() {
for (i = 0 ; i<1000; i++) {
// long code here
}
method2();
}
method2() will be executed before for-loop for unknown reason.
You can call method2() in the callback of your first call, or use Observables to compose the calls to execute one after the other. You can also pass in an Executor that only runs a single task a at a time, although that will apply to every retrofit call.
@f2prateek Could you please point me to Executor manual?
btw. calling method2() in callback inside for-loop leads to multiple call :)
@Kondra007 I'm confused of what is inside your method1()'s body? Why can't you put it in the very last line of the body? Especially if method2 depends on method1 to be successful? I haven't used Executors or Observables but rather do it like that, since any linear method calls usually depend on the first one to succeed.
@jpshelley method1() goes through a HashMap of things and does some network-dependent work (delete posts, create posts and so on).
method2() does some work that needs to be done only after method1() completes.
As far as I see, in this loop, 100 "tasks" are started and loop finishes long before these tasks are completed.
@Kondra007 Just going to say this is more of a basic design problem then retrofit issue then, but If the flow has to be:
method1() -> method1's body of actions -> method2()
then you'll have to know if method1's body has executed properly. Whether you use callbacks on the body's methods, or boolean return values to determine if they were successful is up to you. But it seems like you're not waiting for method1() to complete, rather its entire body which is a completely different rabbit hole.
method1() calls other methods inside itself(One method to create post, one to delete post...)
There's no way to trace callbacks except using global variables. But I'll think it over.
@Kondra007 This might help your issue then: http://stackoverflow.com/a/12416129/1784299
@jpshelley well, that's not my case :(
Your question has nothing to do with Retrofit, this is a general async coding question about composing multiple async calls. There's three basic answers:
java
Observables.combineLatest(firstApiCall(), tenOperations())
.flatMap(secondApiCall())
.subscribe(...);
All three of these are also not Retrofit specific and are outside the scope of this issue (which isn't really an issue but a question). You should direct your questions to a forum like StackOverflow or a mailing list (e.g., RxJava's).
Most helpful comment
Your question has nothing to do with Retrofit, this is a general async coding question about composing multiple async calls. There's three basic answers:
java Observables.combineLatest(firstApiCall(), tenOperations()) .flatMap(secondApiCall()) .subscribe(...);All three of these are also not Retrofit specific and are outside the scope of this issue (which isn't really an issue but a question). You should direct your questions to a forum like StackOverflow or a mailing list (e.g., RxJava's).