Reputation: 1487
Is there a way of know when my Retrofit call has finished it's duty? I like know when all the data is received so the code can move on, like starting another activity or use data from the first call to make a second?
Ps.: I'm using an asynchronous request (.enqueue).
Edit:
getContact(){
//Get a contact List from the server
Call<List<ModelContact>> callM = contactInterface.createRContact(listContact);
callM.enqueue(new Callback<List<ModelContact>>() {
@Override
public void onResponse(Response<List<ModelContact>> response, Retrofit retrofit) {
// Fetch the List from Response and save it on the local database
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
//Gets a object containing some configurations
Call<Configs> callC = contactInterface.getConfigs(Configs);
callC.enqueue(new Callback<Configs>() {
@Override
public void onResponse(Response<Configs> response, Retrofit retrofit) {
// Fetch the Configs object and save it on the database
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
//Here I need to start a new activity after the calls
Intent loginact = new Intent(TokenActivity.this, LoginActivity.class);
startActivity(loginact);
}
Upvotes: 8
Views: 8015
Reputation: 71
If you want to do some task in another activity or another fragment after finishing retrofit call but your activity has changed and you are still waiting on the retrofit call to finish, you can use interface to send data.
Lets say you've an Activity A which is calling an API via retrofit, while this call is executing user has moved to another Activtiy B and now you need to call a method (or do something else) in Activity B upon the completion of the call from A.
Create an interface AB in A, implement AB and override its method in Activity B and call the method via an object of interface AB in activity A inside onResponse() method of the API call .
Upvotes: 0
Reputation: 1634
If you have any request depend each other, it'll be complex. You can do that with RxJava by using flatMap method to chain multiple request and avoid callback hell. it's pretty simple. You can learn it here.
http://joluet.github.io/blog/2014/07/07/rxjava-retrofit/
Upvotes: 0
Reputation: 201
Move
//Gets a object containing some configurations
Call<Configs> callC = contactInterface.getConfigs(Configs);
callC.enqueue(new Callback<Configs>() {
@Override
public void onResponse(Response<Configs> response, Retrofit retrofit) {
// Fetch the Configs object and save it on the database
Intent loginact = new Intent(TokenActivity.this, LoginActivity.class);
startActivity(loginact);
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
inside callM
's onResponse
method like this. That way firs callM
will execute, whenever it finishes callC
will execute, and whenever callC
finishes it will throw the Intent.
getContact(){
//Get a contact List from the server
Call<List<ModelContact>> callM = contactInterface.createRContact(listContact);
callM.enqueue(new Callback<List<ModelContact>>() {
@Override
public void onResponse(Response<List<ModelContact>> response, Retrofit retrofit) {
// Fetch the List from Response and save it on the local database
//Gets a object containing some configurations
Call<Configs> callC = contactInterface.getConfigs(Configs);
callC.enqueue(new Callback<Configs>() {
@Override
public void onResponse(Response<Configs> response, Retrofit retrofit) {
//Fetch the Configs object and save it on the database
//Here I need to start a new activity after the calls
Intent loginact = new Intent(TokenActivity.this, LoginActivity.class);
startActivity(loginact);
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
}
@Override
public void onFailure(Throwable t) {
Log.i("TAG", "Error: " + t.getMessage());
}
});
}
Upvotes: 1
Reputation: 503
Perhaps you can use two boolean flags and start the new intent outside of your getContact method.
Something like this:
public class MyActivity extends Activity {
//lot of code omitted
private boolean cIsFinished;
private boolean mIsFinished;
private void getContact(){
//create M and C
m.onResponse(){
//do whatever you want with data and call startIntent
mIsFinished=true;
startIntent();
}
c.onResponse(){
//do whatever you want with data and call startIntent
cIsFinished=true;
startIntent();
}
}
private synchronized void startIntent(){
if(cIsFinished && mIsFinished){
//startIntentHere!
Intent intent = new blah blah blah
}
}
}
Upvotes: 5