2016-10-24 3 views
1

J'ai deux méthodes dans mon TcpClient. le premier est startListener, et après cela, je fais connect. les deux retournent void.android - en utilisant les méthodes continueWith et onSuccess de BoltsFramework avec les méthodes void

Dans mon implémentation TcpClient actuelle, l'application se bloque si je fais startListener puis connect juste après (je suppose qu'il doit y avoir un certain temps entre eux?). La mise en œuvre est here, à partir de SimpleTCPLibrary (il fait startListener dans onStart(), et a un connecter bouton qui déclenche connect).

Ce que je veux faire est de faire startListener, et quand est fini avec succès -> faire connect. Je n'ai pas trouvé d'exemples montrant comment faire cela en utilisant BoltsFramework'scontinueWith ou onSuccess.

Existe-t-il des exemples?

+0

-ce que [cette aide] (https://github.com/Zhuinden/realm-databind -experiment/blob/maître/app/src/principal/java/com/zhuinden/realmdatabind/trucs/WriteElements.java # L23-L30)? – EpicPandaForce

Répondre

1

Vous pouvez toujours essayer

Task.delay(200).continueWith(new Continuation<Void, Object>() { 
    @Override 
    public Object then(Task<Void> task) throws Exception { 
     ... connect(); 
     return null; 
    } 
}); 
1
Task.callInBackground(new Callable<Void>() { //or `Task.call` for synchronous 
     @Override 
     public Void call() 
       throws Exception { 
      /*... startListener */ 
      return null; 
     } 
    }).continueWithTask(new Continuation<Void, Task<Void>>() { 
     @Override 
     public Task<Void> then(Task<Void> ignored) 
       throws Exception { 
      return Task.delay(200); 
     } 
    }).continueWith(new Continuation<Void, Void>() { 
     @Override 
     public Void then(Task<Void> ignored) 
       throws Exception { 
      /*... connect */ 
      return null; 
     } 
    }); 

ou lambdas:

Task.call(() -> { TcpClient.startListener(); return null; }) 
    .continueWithTask(ignored -> Task.delay(200)) 
    .continueWith(ignored -> { TcpClient.connect(); return null; });