Stutern Graduate Accelerator 0.7 Android development guest session - Introduction to Multithreading and Networking on Android

Stutern Graduate Accelerator 0.7 Android development guest session - Introduction to Multithreading and Networking on Android

In this presentation, I introduced two programming concepts to beginner Android developers in the Stutern Graduate Accelerator 0.7 Android development track. I explored Multithreading and Networking on Android with RxJava and Retrofit.

I also shared some career advice based on my experience. It touched on topics such as figuring out what employers are looking out for, learning how to build a career portfolio as an Android developer and how to find job opportunities.

E803718649600ddffc1bc625d957e786?s=128

Moyinoluwa Adeyemi

February 18, 2020
Tweet

Transcript

  1. Multithreading and Networking on Android Moyinoluwa Adeyemi + career advice

  2. Android Google Developer Expert

  3. Multithreading with RxJava

  4. Activity/Fragment (A/F) Service (S) Content Providers (CP) Broadcast Receivers (BR)

    UI/Main Thread Android Components
  5. A/F S Activity/Fragment (A/F) Service (S) Content Providers (CP) Broadcast

    Receivers (BR) BR CP A/F Message Queue Android Components
  6. A/F S BR CP A/F } 10ms } 13ms }

    16ms } 15ms } 4ms 60fps
  7. A/F S BR CP A/F } 10ms } 13ms }

    16ms } 20ms } 4ms Jank
  8. Common causes • I/O operations (Network requests, Database Access)

  9. Common causes • I/O operations (Network requests, Database Access) •

    Long calculations (Image processing)
  10. The result https://developer.android.com/topic/performance/images/anr-example-framed.png

  11. Why bother? ⭐⭐⭐⭐⭐

  12. Why bother? ⭐⭐⭐⭐⭐

  13. Why bother? ⭐⭐⭐⭐⭐ ⏱⏱⏱⏱⏱

  14. Solution - Android AsyncTask

  15. AsyncTask • Impossible to cancel a task

  16. AsyncTask • Impossible to cancel a task • Potential memory

    leaks
  17. Solution - Kotlin Coroutines

  18. Coroutines • Beyond the scope of this talk, sadly…

  19. Solution - Open Source library

  20. RxJava “RxJava is a Java VM implementation of ReactiveX a

    library for composing asynchronous and event-based programs by using observable sequences.”
  21. RxJava Subscriber Observable subscriber.onNext()

  22. RxJava - Schedulers “Executes arbitrary blocks of code, possibly in

    the future”
  23. RxJava - Schedulers “Executes arbitrary blocks of code, possibly in

    the future” Schedulers + subscribeOn()
  24. RxJava - Schedulers “Executes arbitrary blocks of code, possibly in

    the future” Schedulers + subscribeOn() + observeOn()
  25. Schedulers.io() • Unbounded pool of threads

  26. Schedulers.io() • Unbounded pool of threads • Threads are recycled

  27. Schedulers.io() • Unbounded pool of threads • Threads are recycled

    • Used for I/O bound tasks
  28. Schedulers.io() • Unbounded pool of threads • Threads are recycled

    • Used for I/O bound tasks • Tasks take some time to complete
  29. Schedulers.computation() • Bounded pool of threads

  30. Schedulers.computation() • Bounded pool of threads • Used for CPU

    intensive tasks
  31. Schedulers.computation() • Bounded pool of threads • Used for CPU

    intensive tasks • Computation threads limited to the number of CPU cores
  32. Schedulers.trampoline() • Schedules tasks in the same thread

  33. Schedulers.trampoline() • Schedules tasks in the same thread • Blocking,

    but waits for the current task to finish
  34. subscribeOn() • Determines the thread which the source observable emits

    on
  35. subscribeOn() • Determines the thread which the source observable emits

    on • Works upstream - only the first call counts
  36. subscribeOn() What thread will the source observable emit on? Observable.just("Hello",

    "World") .subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.computation()) ...
  37. observeOn() • Switches the current observable to the specified thread

  38. observeOn() • Switches the current observable to the specified thread

    • Works downstream - only the last call counts
  39. observeOn() What thread will the current observable be emitted on?

    Observable.just("Hello", "World") .subscribeOn(Schedulers.io()) .observeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) ...
  40. TestScheduler “A special, non thread-safe scheduler for testing operators that

    require a scheduler without introducing real concurrency and allows manually advancing a virtual time.”
  41. TestScheduler - UseCase Observable.interval(ONE_SECOND, TimeUnit.SECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ ..... })

  42. TestScheduler - UseCase private val testScheduler = TestScheduler()

  43. private val testScheduler = TestScheduler() @Before fun setUp() { RxJavaPlugins.setComputationSchedulerHandler

    { testScheduler } } TestScheduler - UseCase
  44. @Test fun test_shouldReturnSuccess_whenRequestIsSuccessful() { viewModel.result.observeForever(observer) testScheduler.advanceTimeBy(1, TimeUnit.SECONDS) verify(observer).onChanged(...) } TestScheduler

    - UseCase
  45. AndroidSchedulers.mainThread() • Available only in RxAndroid

  46. AndroidSchedulers.mainThread() • Available only in RxAndroid • Performs Android UI

    based work on the main thread
  47. Import RxJava in Gradle implementation "io.reactivex.rxjava2:rxjava:$rx_java2_version" implementation "io.reactivex.rxjava2:rxandroid:$rx_android_version"

  48. Networking with Retrofit

  49. Retrofit • Open source library for making HTTP requests

  50. Retrofit • Open source library for making HTTP requests •

    Accepts an HTTP client
  51. Retrofit • Open source library for making HTTP requests •

    Accepts an HTTP client • Accepts a Call Adapter factory
  52. Retrofit • Open source library for making HTTP requests •

    Accepts an HTTP client • Accepts a Call Adapter factory • Accepts a Converter factory
  53. Retrofit - Converters • Gson: com.squareup.retrofit2:converter-gson • Jackson: com.squareup.retrofit2:converter-jackson •

    Moshi: com.squareup.retrofit2:converter-moshi • Protobuf: com.squareup.retrofit2:converter-protobuf • Wire: com.squareup.retrofit2:converter-wire • Simple XML: com.squareup.retrofit2:converter-simplexml • Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
  54. Retrofit + OkHttp val okHttpClient = OkHttpClient.Builder() .build()

  55. Retrofit + OkHttp Retrofit.Builder() .client(okHttpClient)

  56. Retrofit Retrofit.Builder() .client(okHttpClient) .baseUrl(BuildConfig.BASE_URL)

  57. Retrofit Retrofit.Builder() .client(okHttpClient) .baseUrl(BuildConfig.BASE_URL) .addConverterFactory(MoshiConverterFactory.create(moshi))

  58. Retrofit Retrofit.Builder() .client(okHttpClient) .baseUrl(BuildConfig.BASE_URL) .addConverterFactory(MoshiConverterFactory.create(moshi)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build()

  59. Retrofit - Annotations GET POST PUT DELETE HEAD

  60. Retrofit - Interface https://demo.sga.org/latest?town=lagos interface DemoService { @GET("/latest") fun getLatestDemo(@Query("town")

    town: String): Observable<Demo> }
  61. Retrofit - Interface https://demo.sga.org/latest?town=lagos interface DemoService { @GET("/latest") fun getLatestDemo(@Query("town")

    town: String): Observable<Demo> } val demoService = retrofit.create(DemoService::class.java)
  62. Retrofit + RxJava (Multithreading) https://demo.sga.org/latest?town=lagos demoService.getLatestDemo("lagos") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())

  63. Retrofit + RxJava (Error handling) https://demo.sga.org/latest?town=lagos demoService.getLatestDemo("lagos") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({

    demo -> ...}, { error -> ... })
  64. Import Retrofit in Gradle // moshi implementation "com.squareup.moshi:moshi-kotlin:$moshi_version" implementation "com.squareup.moshi:moshi-adapters:$moshi_version"

    // Retrofit implementation "com.squareup.retrofit2:retrofit:$retrofit_version" implementation "com.squareup.retrofit2:converter-moshi:$moshi_converter_version" implementation "com.squareup.retrofit2:adapter-rxjava2:$rx_java_adapter_version"
  65. Career Section

  66. What are employers looking for?

  67. What are employers looking for? • Technical Experience suitable for

    the role
  68. What are employers looking for? • Technical Experience suitable for

    the role • “Soft” skills
  69. What are employers looking for? • Technical Experience suitable for

    the role • “Soft” skills • Growth potential
  70. What are employers looking for? • Technical Experience suitable for

    the role • “Soft” skills • Growth potential • Bonus - Writing skills
  71. What are employers looking for? • Technical Experience suitable for

    the role • “Soft” skills • Growth potential • Bonus - Writing skills • Bonus - Contributions to the community
  72. Building a career portfolio

  73. Building a career portfolio • Contribute to an existing project

  74. Building a career portfolio • Contribute to an existing project

    • Upload code snippets or sample apps to Github
  75. Building a career portfolio • Contribute to an existing project

    • Upload code snippets or sample apps to Github • Publish apps on Google Play
  76. Building a career portfolio • Contribute to an existing project

    • Upload code snippets or sample apps to Github • Publish apps on Google Play • Write technical articles
  77. Building a career portfolio • Contribute to an existing project

    • Upload code snippets or sample apps to Github • Publish apps on Google Play • Write technical articles • Give talks
  78. How to find opportunities

  79. How to find opportunities • Social media (LinkedIn, Twitter, StackOverflow

    Jobs)
  80. How to find opportunities • Social media (LinkedIn, Twitter, StackOverflow

    Jobs) • Newsletters (Forloop, Android/Kotlin Weekly)
  81. How to find opportunities • Social media (LinkedIn, Twitter, StackOverflow

    Jobs) • Newsletters (Forloop, Android/Kotlin Weekly) • Job Search marketplace (Hired.com, Fiverr, Upwork)
  82. How to find opportunities • Social media (LinkedIn, Twitter, StackOverflow

    Jobs) • Newsletters (Forloop, Android/Kotlin Weekly) • Job Search marketplace (Hired.com, Fiverr, Upwork) • Company Career pages
  83. How to find opportunities • Social media (LinkedIn, Twitter, StackOverflow

    Jobs) • Newsletters (Forloop, Android/Kotlin Weekly) • Job Search marketplace (Hired.com, Fiverr, Upwork) • Company Career pages • Employee referrals
  84. Questions?

  85. Resources • https://developer.android.com/training/articles/perf-anr • http://hvasconcelos.github.io/articles/Offloading-work-from-the- UI-Thread • https://blog.gojekengineering.com/multi-threading-like-a-boss-in -android-with-rxjava-2-b8b7cf6eb5e2 •

    https://square.github.io/retrofit/ • Reactive Programming with RxJava - Tomasz Nurkiewicz
  86. Take home task

  87. Take home task Create a one page app with GitHub’s

    public REST API, listing all names of users in Lagos. The app should make use of both RxJava and Retrofit for networking. Make the network request on the io thread. For bonus points, use an Android App architecture and write tests.
  88. Thank you!