2016-01-12 13 views
6

C'è un modo per sapere quando è terminata la mia chiamata di retrofit? Mi piace sapere quando tutti i dati sono ricevuti in modo che il codice possa passare, come iniziare un'altra attività o utilizzare i dati della prima chiamata per fare un secondo?Come sapere quando una chiamata di aggiornamento è terminata

Ps: sto utilizzando una richiesta asincrona (.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); 


} 
+0

è possibile fornire un delegato per accodare (aka ascoltatore). – Blackbelt

+0

Dove posso trovare un esempio di codice, l'ho creato e non ho trovato nulla ... – Rafael

+1

perché non pubblichi il tuo tentativo? – Blackbelt

risposta

3

Forse è possibile utilizzare due bandiere booleani e iniziare il nuovo intento di fuori del proprio metodo di getContact.

Qualcosa di simile a questo:

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 
     } 

    }  
} 
+0

Utilizzando questo metodo quando il codice raggiunge startIntent() e le risposte non sono ancora state ricevute, si fermerà qui, ho bisogno che sia reattivo, come quando le variabili sono impostate su true, rende di nuovo il test ... Un modo per farlo? – Rafael

1

Spostare

//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()); 
    } 
}); 

all'interno onResponse metodo callM s' come questo. In questo modo verrà eseguito il primo numero callM, ogni volta che verrà completato callC verrà eseguito e ogni volta che termina verrà lanciato l'intento.

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()); 
     } 
    });  
    } 
+0

Sto cercando un po 'di non entrare in un Callback Hell, perché in seguito sarò costretto a fare la stessa cosa usando 8 differenziali ent chiama, e impostandone uno all'interno di un altro diventerà un po 'disordinato ... Penso che il link di riferimento – Rafael

Problemi correlati