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

Les AsyncTasks

Les AsyncTasks

Souleymane Sidibe

February 15, 2014
Tweet

More Decks by Souleymane Sidibe

Other Decks in Programming

Transcript

  1. Les AsyncTasks

    View full-size slide

  2. Plan
    L’AsyncTask
    Les limites
    Android c’est du java non?
    La solution magique
    Bonus

    View full-size slide

  3. Un passionné d’android
    #nexus #startup #ios
    #java #play! #scala #ps3
    #pes #techno #android #devops #BARCA
    #photos #google #api
    twitter: @soulesidibe
    gplus: plus.google.com/+souleymanesidibe

    View full-size slide

  4. L’AsyncTask

    View full-size slide

  5. ● Traitement en background et mise a jour du
    UI
    ● Pas besoin de toucher aux Threads!
    AsyncTask.java

    View full-size slide

  6. Pourquoi?
    AsyncTask.java

    View full-size slide

  7. Pourquoi?
    ● Ne pas bloquer le UI Thread!
    AsyncTask.java

    View full-size slide

  8. mAsync.cancel(true);

    View full-size slide

  9. YourAsyncTask1 task1 = new YourAsyncTask1();
    task1.execute(params);
    YourAsyncTask2 task2 = new YourAsyncTask2();
    task2.execute(params);
    Question: Serial or parallel?

    View full-size slide

  10. Android c’est du java
    non?

    View full-size slide

  11. Le framework Executor
    Executor executor = Executors.newSingleThreadExecutor();
    executor.execute(new MyRunnable());

    View full-size slide

  12. Le framework Executor
    Executor executor = Executors.newSingleThreadExecutor();
    executor.execute(new MyRunnable());
    ExecutorService execute = Executors.newSingleThreadExecutor();
    Future future = execute.submit(new MonCallable());

    View full-size slide

  13. Le framework Executor
    Executor executor = Executors.newSingleThreadExecutor();
    executor.execute(new MyRunnable());
    ExecutorService execute = Executors.newSingleThreadExecutor();
    Future future = execute.submit(new MonCallable());
    ScheduledExecutorService execute = Executors.
    newSingleThreadScheduledExecutor();
    execute.scheduleAtFixedRate(new MonRunnable(), 0, 1, TimeUnit.SECONDS);

    View full-size slide

  14. La solution magique

    View full-size slide

  15. Fragment
    Activity

    View full-size slide

  16. Fragment
    Activity
    Thread
    in progress

    View full-size slide

  17. Fragment
    Activity
    Thread
    Done

    View full-size slide

  18. Fragment
    Activity
    Thread
    Done
    BUS
    POST
    MyEvent

    View full-size slide

  19. EventB
    MyEvent
    Thread
    Done
    BUS
    POST
    MyEvent
    MyEvent
    EventA
    EventB
    EventA Subscribe
    Subscribe
    Subscribe

    View full-size slide

  20. Event Bus
    Otto (goo.gl/sWxXY)
    EventBus (goo.gl/nAEW6)
    rxJava (goo.gl/TeWxWr)

    View full-size slide

  21. Otto
    ● Square (goo.gl/VlqRcA)
    ● guava-based event bus
    ● Open Source
    ● easy to use

    View full-size slide

  22. YourAsyncTask1 task1 = new YourAsyncTask1();
    task1.executeOnExecutor(executor, params);
    YourAsyncTask2 task2 = new YourAsyncTask2();
    task2.executeOnExecutor(executor, params);
    Astuce

    View full-size slide

  23. Communication entre Composants
    ● Activity - Fragment
    ● Fragment - Fragment
    Otto, c’est doux!

    View full-size slide

  24. Communication entre Composants
    Activity
    Fragment A Fragment B
    BUS

    View full-size slide

  25. Links
    ● #doc AsyncTask (goo.gl/MRbjQD)
    ● #post The dark side of AsyncTask (goo.
    gl/mm8IVR)
    ● #tuto Le Framework Executor(goo.
    gl/MCJLq4)
    ● #talk Android Bad Practices : comment
    foirer son app(goo.gl/Ix8Jsn)

    View full-size slide