Slide 1

Slide 1 text

Multithreading and Networking on Android Moyinoluwa Adeyemi + career advice

Slide 2

Slide 2 text

Android Google Developer Expert

Slide 3

Slide 3 text

Multithreading with RxJava

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

A/F S Activity/Fragment (A/F) Service (S) Content Providers (CP) Broadcast Receivers (BR) BR CP A/F Message Queue Android Components

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

A/F S BR CP A/F } 10ms } 13ms } 16ms } 20ms } 4ms Jank

Slide 8

Slide 8 text

Common causes ● I/O operations (Network requests, Database Access)

Slide 9

Slide 9 text

Common causes ● I/O operations (Network requests, Database Access) ● Long calculations (Image processing)

Slide 10

Slide 10 text

The result https://developer.android.com/topic/performance/images/anr-example-framed.png

Slide 11

Slide 11 text

Why bother? ⭐⭐⭐⭐⭐

Slide 12

Slide 12 text

Why bother? ⭐⭐⭐⭐⭐

Slide 13

Slide 13 text

Why bother? ⭐⭐⭐⭐⭐ ⏱⏱⏱⏱⏱

Slide 14

Slide 14 text

Solution - Android AsyncTask

Slide 15

Slide 15 text

AsyncTask ● Impossible to cancel a task

Slide 16

Slide 16 text

AsyncTask ● Impossible to cancel a task ● Potential memory leaks

Slide 17

Slide 17 text

Solution - Kotlin Coroutines

Slide 18

Slide 18 text

Coroutines ● Beyond the scope of this talk, sadly…

Slide 19

Slide 19 text

Solution - Open Source library

Slide 20

Slide 20 text

RxJava “RxJava is a Java VM implementation of ReactiveX a library for composing asynchronous and event-based programs by using observable sequences.”

Slide 21

Slide 21 text

RxJava Subscriber Observable subscriber.onNext()

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Schedulers.io() ● Unbounded pool of threads

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Schedulers.io() ● Unbounded pool of threads ● Threads are recycled ● Used for I/O bound tasks ● Tasks take some time to complete

Slide 29

Slide 29 text

Schedulers.computation() ● Bounded pool of threads

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Schedulers.computation() ● Bounded pool of threads ● Used for CPU intensive tasks ● Computation threads limited to the number of CPU cores

Slide 32

Slide 32 text

Schedulers.trampoline() ● Schedules tasks in the same thread

Slide 33

Slide 33 text

Schedulers.trampoline() ● Schedules tasks in the same thread ● Blocking, but waits for the current task to finish

Slide 34

Slide 34 text

subscribeOn() ● Determines the thread which the source observable emits on

Slide 35

Slide 35 text

subscribeOn() ● Determines the thread which the source observable emits on ● Works upstream - only the first call counts

Slide 36

Slide 36 text

subscribeOn() What thread will the source observable emit on? Observable.just("Hello", "World") .subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.computation()) ...

Slide 37

Slide 37 text

observeOn() ● Switches the current observable to the specified thread

Slide 38

Slide 38 text

observeOn() ● Switches the current observable to the specified thread ● Works downstream - only the last call counts

Slide 39

Slide 39 text

observeOn() What thread will the current observable be emitted on? Observable.just("Hello", "World") .subscribeOn(Schedulers.io()) .observeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) ...

Slide 40

Slide 40 text

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.”

Slide 41

Slide 41 text

TestScheduler - UseCase Observable.interval(ONE_SECOND, TimeUnit.SECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ ..... })

Slide 42

Slide 42 text

TestScheduler - UseCase private val testScheduler = TestScheduler()

Slide 43

Slide 43 text

private val testScheduler = TestScheduler() @Before fun setUp() { RxJavaPlugins.setComputationSchedulerHandler { testScheduler } } TestScheduler - UseCase

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

AndroidSchedulers.mainThread() ● Available only in RxAndroid

Slide 46

Slide 46 text

AndroidSchedulers.mainThread() ● Available only in RxAndroid ● Performs Android UI based work on the main thread

Slide 47

Slide 47 text

Import RxJava in Gradle implementation "io.reactivex.rxjava2:rxjava:$rx_java2_version" implementation "io.reactivex.rxjava2:rxandroid:$rx_android_version"

Slide 48

Slide 48 text

Networking with Retrofit

Slide 49

Slide 49 text

Retrofit ● Open source library for making HTTP requests

Slide 50

Slide 50 text

Retrofit ● Open source library for making HTTP requests ● Accepts an HTTP client

Slide 51

Slide 51 text

Retrofit ● Open source library for making HTTP requests ● Accepts an HTTP client ● Accepts a Call Adapter factory

Slide 52

Slide 52 text

Retrofit ● Open source library for making HTTP requests ● Accepts an HTTP client ● Accepts a Call Adapter factory ● Accepts a Converter factory

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

Retrofit + OkHttp val okHttpClient = OkHttpClient.Builder() .build()

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

Retrofit - Annotations GET POST PUT DELETE HEAD

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Retrofit - Interface https://demo.sga.org/latest?town=lagos interface DemoService { @GET("/latest") fun getLatestDemo(@Query("town") town: String): Observable } val demoService = retrofit.create(DemoService::class.java)

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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"

Slide 65

Slide 65 text

Career Section

Slide 66

Slide 66 text

What are employers looking for?

Slide 67

Slide 67 text

What are employers looking for? ● Technical Experience suitable for the role

Slide 68

Slide 68 text

What are employers looking for? ● Technical Experience suitable for the role ● “Soft” skills

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

What are employers looking for? ● Technical Experience suitable for the role ● “Soft” skills ● Growth potential ● Bonus - Writing skills ● Bonus - Contributions to the community

Slide 72

Slide 72 text

Building a career portfolio

Slide 73

Slide 73 text

Building a career portfolio ● Contribute to an existing project

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

How to find opportunities

Slide 79

Slide 79 text

How to find opportunities ● Social media (LinkedIn, Twitter, StackOverflow Jobs)

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

Questions?

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

Take home task

Slide 87

Slide 87 text

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.

Slide 88

Slide 88 text

Thank you!