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

mDevCamp: RxJava & Coroutines: A Practical Anal...

mDevCamp: RxJava & Coroutines: A Practical Analysis

Kotlin has taken the Android world by storm, and is quickly becoming the most popular language, with coroutines now stable, does it make sense to replace your RxJava implementations for Coroutines?

Despite the hype, it may not make sense to jump on the bandwagon just yet, with RxJava having already proven its stability and usefulness, and in many cases the comparison between the two frameworks showing that they simply fit different purposes.

In this talk, you can learn how you can utilise the strengths of each framework, how to correctly choose the best solution for the requirements of your project, whether it may be beneficial to migrate or run them concurrently, and how you can start doing so for your project.

Ash Davies

May 31, 2019
Tweet

More Decks by Ash Davies

Other Decks in Programming

Transcript

  1. Coroutine Builders val deferred: Deferred<String> = async { "Hello World!"

    } val result: String = deferred.await() @askashdavies
  2. Coroutine Builders val deferred: Deferred<String> = async { "Hello World!"

    } val result: String = deferred.await() val job: Job = launch { "Hello World!" } @askashdavies
  3. Coroutine Builders val deferred: Deferred<String> = async { "Hello World!"

    } val result: String = deferred.await() val job: Job = launch { "Hello World!" } job.join() @askashdavies
  4. ! Observable .fromIterable(resourceDraft.getResources()) .flatMap(resourceServiceApiClient::createUploadContainer) .zipWith(Observable.fromIterable(resourceDraft.getResources()), Pair::create) .flatMap(uploadResources()) .toList() .toObservable() .flatMapMaybe(resourceCache.getResourceCachedItem())

    .defaultIfEmpty(Resource.getDefaultItem()) .flatMap(postResource(resourceId, resourceDraft.getText(), currentUser, resourceDraft.getIntent())) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe( resource -> repository.setResource(resourceId, resource, provisionalResourceId), resourceUploadError(resourceId, resourceDraft, provisionalResourceId) ); @askashdavies
  5. "If all you have is a hammer, everything looks like

    a nail" — Abraham Maslow, The Psychology of Science, 1966 @askashdavies
  6. Network Call Handling // RxJava2: Single<T> fun getUser(): Single<User> =

    Single.fromCallable { /* ... */ } // Coroutines: T suspend fun getUser(): User = /* ... */ @askashdavies
  7. Cache Retrieval // RxJava2: Maybe<T> fun getUser(): Maybe<User> = Maybe.fromCallable

    { /* ... */ } // Coroutines: T? suspend fun getUser(): User? = /* ... */ @askashdavies
  8. Background Task Handling // RxJava2: Completable fun storeUser(user: User): Completable.fromCallable

    { /* ... */ } // Coroutines: Unit suspend fun storeUser(user: User) { /* ... */ } @askashdavies
  9. Thread Handling // RxJava2 getUser() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { /*

    Do something */ } // Coroutines launch(Dispatchers.Main) { withContext(Dispatchers.IO) { val user = getUser() /* Do something */ } } @askashdavies
  10. Task Cancellation // RxJava2 val disposable: Disposable = Single .create

    { /* Do something */ } .subscribe { /* Do something else */ } disposable.dispose() // Coroutine val parent: Job = Job() launch(Dispatchers.Main + parent) { /* Do something */ } parent.cancelChildren() @askashdavies
  11. Roman Elizarov Cold flows, hot channels Flow.collect { it.emit("Hello World!")

    } medium.com/@elizarov/cold-flows-hot-channels-d74769805f9
  12. "If it ain't broke, don't fix it" — Bert Lance,

    Nation's Business, 1977 @askashdavies
  13. interface UserService { @GET("/user") fun getUser(): Deferred<User> } val retrofit

    = Retrofit.Builder() .baseUrl("https://example.com/") .addCallAdapterFactory(CoroutineCallAdapterFactory()) .build() GlobalScope.launch { val user = retrofit .create<UserService>() // >= 2.5.0 .getUser() .await() } @askashdavies
  14. interface UserService { @GET("/user") fun getUser(): Deferred<User> } val retrofit

    = Retrofit.Builder() .baseUrl("https://example.com/") .addCallAdapterFactory(CoroutineCallAdapterFactory()) .build() GlobalScope.launch { val user = retrofit .create<UserService>() // >= 2.5.0 .getUser() .await() } @askashdavies
  15. Name Scope Description rxCompletable CoroutineScope Cold completable that starts coroutine

    on subscribe rxMaybe CoroutineScope Cold maybe that starts coroutine on subscribe rxSingle CoroutineScope Cold single that starts coroutine on subscribe rxObservable ProducerScope Cold observable that starts coroutine on subscribe rxFlowable ProducerScope Cold observable that starts coroutine on subscribe with backpressure support @askashdavies
  16. val service: UserService = retrofit.create() // >= 2.5.0 GlobalScope .rxSingle

    { service.getUser() } .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe( { /* Do something with user */ }, { /* Handle error ... probably */ } ) @askashdavies
  17. Maybe.await() GlobalScope.launch { val result: String? = Maybe .fromCallable {

    null as String? } .await() // result == null } @askashdavies
  18. Observable.await... val observable = Observable.just(1, 2, 3, 4) // Await

    first item val item = observable.awaitFirst() // Print each item observable.consumeEach(::println) // Consume all items observable .openSubscription() .consume { println(it.size) } kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/io.reactivex.-observable-source/index.html
  19. Exceptions observable.subscribe( onSuccess = { it: T -> }, onError

    = { it: Throwable -> } // Gotta catch em all! ) launch { try { doSomethingDangerous() } catch(exception: DangerousException) { // Specific exception caught! } } @askashdavies
  20. Exceptions val handler = CoroutineExceptionHandler { _, exception -> println("Caught

    $exception") } val job = GlobalScope.launch(handler) { throw AssertionError() } join(job) // Caught java.lang.AssertionError @askashdavies