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

Asynchronous Programming: Journey from RxJava t...

Asynchronous Programming: Journey from RxJava to Kotlin Coroutines

Idorenyin Obong

July 27, 2019
Tweet

More Decks by Idorenyin Obong

Other Decks in Programming

Transcript

  1. “One can think of a coroutine as a light- weight

    thread. Like threads, coroutines can run in parallel, wait for each other and communicate. The biggest difference is that coroutines are very cheap, almost free: we can create thousands of them, and pay very little in terms of performance. True threads, on the other hand, are expensive to start and keep around. A thousand threads can be a serious challenge for a modern machine.” COROUTINES
  2. “A coroutine is a way to handle concurrency tasks in

    a thread. It will run until stopped, and the thread will change the context to each coroutine without creating new threads.” COROUTINES
  3. “Rx code is readable, however we still pass callbacks which

    doesn’t represent our mind model (we think sequentially)”
  4. Why you should use Coroutines • Clean sequential code •

    Handles backpressure • Third-party libraries support
  5. Why you should use Coroutines • Clean sequential code •

    Handles backpressure • Third-party libraries support • Easier to create custom operators
  6. Why you should use Coroutines • Clean sequential code •

    Handles backpressure • Third-party libraries support • Easier to create custom operators • More performant.
  7. Why you should use Coroutines • Clean sequential code •

    Handles backpressure • Third-party libraries support • Easier to create custom operators • More performant. • Easier to learn
  8. @GET("/positions.json?description=web")
 fun getWebJobs(@Query("page") page: Int): Single<List<JobModel>> 
 private val disposables

    = CompositeDisposable()
 private val observer = object: DisposableSingleObserver<List<JobModel>>() {
 override fun onSuccess(t: List<JobModel>) {}
 override fun onError(e: Throwable) {}
 } 
 fetchWebJobs()
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribeWith<DisposableSingleObserver<List<JobModel>>>(observer)
 .addTo(disposables)
  9. Best practices when using Coroutines • Try to avoid GlobalScope

    • Try to avoid the async coroutine • Use the Dispatchers.Main as main coroutine
  10. val uiScope = CoroutineScope(Dispatchers.Main)
 val bgDispatcher = Dispatchers.IO bgDispatcher.launch {

    
 
 withContext(uiScope){ 
 // update UI
 }
 
 val result = // blocking call 
 
 withContext(uiScope){ 
 // update UI
 } } AVOID
  11. val uiScope = CoroutineScope(Dispatchers.Main)
 val bgDispatcher = Dispatchers.IO uiScope.launch {

    
 // updating UI
 
 val result = withContext(bgDispatcher){ 
 // blocking call
 }
 
 // update UI } RECOMMEND
  12. Best practices when using Coroutines • Try to avoid GlobalScope

    • Try to avoid the async coroutine • Use the Dispatchers.Main as main coroutine • Always handle exceptions