Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous Injection

Asynchronous Injection

8928b5277aa7234dbca4ff782952343a?s=128

funnelbit

June 22, 2016
Tweet

Transcript

  1. "TZODISPOPVT*OKFDUJPO JEGVOOFMCJU

  2. ࣗݾ঺հ ๺ଜྋ )BUFOBGVOOFMCJU 5XJUUFS!FYQFSPQFSP

  3. None
  4. "OESPJE Ͱ ඇಉظ ʹ ґଘղܾ͢Δ࿩

  5. %BHHFS 1SPEVDFST w ඇಉظͰґଘղܾ͢Δ࢓૊ΈΛఏڙ

  6. ؆୯ͳྫ w .PEVMFº w $PNQPOFOU w "DUJWJUZ

  7. .PEVMF @ProducerModule
 final class AppProducerModule {
 @Produces
 ListenableFuture<UserData> getUserData() {


    // ~ ͳΜ͔ΊͬͪΌ͕͔͔࣌ؒΔॲཧ ~
 return Futures.immediateFuture(new UserData("userName"));
 }
 
 @Produces
 String userName(UserData userData) { return userData.name; // Activity ͸͜Ε͕΄͍͠
 }
  8. .PEVMF @ProducerModule
 final class AppProducerModule {
 @Produces
 ListenableFuture<UserData> getUserData() {

    // 1
 // ~ ͳΜ͔ΊͬͪΌ͕͔͔࣌ؒΔॲཧ ~
 return Futures.immediateFuture(new UserData("userName"));
 }
 
 @Produces
 String userName(UserData userData) { // 2 return userData.name; // Activity ͸͜Ε͕΄͍͠
 }
  9. .PEVMF @Module
 public class ExecutorModule {
 @Provides
 @Production
 static Executor

    executor() {
 return Executors.newCachedThreadPool();
 }
 }
  10. $PNQPOFOU @ProductionComponent(modules = { ExecutorModule.class, AppProducerModule.class})
 public interface AppProducerComponent {


    ListenableFuture<String> userName();
 }
  11. "DUJWJUZ … ListenableFuture<String> userDataListenableFuture = DaggerAppProducerComponent.create().userName();
 Futures.addCallback( userDataListenableFuture, new FutureCallback<String>()

    {
 @Override
 public void onSuccess(String result) {
 Log.e("name", result);
 }
 
 @Override
 public void onFailure(Throwable t) {
 Log.e("failure", t.toString());
 }
 }); …
  12. 1SPEVDFS5

  13. .PEVMF @ProducerModule
 public class AppProducerLazyModule {
 @Produces @Normal
 ListenableFuture<UserData> provideNUserData()

    {
 return Futures.immediateFuture(new UserData("normal"));
 }
 
 @Produces @Special
 ListenableFuture<UserData> provideSUserData() {
 return Futures.immediateFuture(new UserData("special"));
 } 
 @Produces
 ListenableFuture<UserData> provideUserData( @Normal Producer<UserData> nProducer, @Special Producer<UserData> sProducer) {
 return sProducer.get();
 }
 }
  14. @ProducerModule public class MyModule { @Produces ListenableFuture<A> a() { …

    } @Produces ListenableFuture<B> b(A a) { … } @Produces ListenableFuture<C> c(B b) { … } @Produces @Delayed ListenableFuture<C> delayedC(A a, Producer<C> c) { … return c.get(); } }
  15. @ProducerModule public class MyModule { @Produces ListenableFuture<A> a() { …

    } // 1 @Produces ListenableFuture<B> b(A a) { … } // 3 @Produces ListenableFuture<C> c(B b) { … } // 4 @Produces @Delayed ListenableFuture<C> delayedC(A a, Producer<C> c) { … return c.get(); } // 2 }
  16. 1SPEVDFE5

  17. .PEVMF @ProducerModule
 final class AppProducerModule {
 @Produces
 ListenableFuture<UserData> getUserData() {


    // ~ ͳΜ͔ΊͬͪΌ͕͔͔࣌ؒΔॲཧ ~
 throw new IllegalStateException(); // Θ͟ͱམͱ͢
 }
 
 @Produces
 String userName(Produced<UserData> userData) {
 try {
 return userData.get().name;
 } catch (ExecutionException e) {
 e.printStackTrace();
 return "Կ͔͕ى͖ͨ";
 }
 }
 }
  18. ৄࡉ w IUUQHPPHMFHJUIVCJPEBHHFSQSPEVDFST

  19. ͋Γ͕ͱ͏͍͟͝·ͨ͠