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

RxJava vs Coroutines

Hadi Tok
November 04, 2018

RxJava vs Coroutines

Hadi Tok

November 04, 2018
Tweet

More Decks by Hadi Tok

Other Decks in Programming

Transcript

  1. Data layer Business Logic Layer UI Layer Observable (Single, Completable,

    Maybe) Observable (Single, Completable, Maybe) I/O (Network-File-DB)
  2. Data layer Business Logic Layer UI Layer Observable (Single, Completable,

    Maybe) Observable (Single, Completable, Maybe) Observable (Single, Completable, Maybe) I/O (Network-File-DB)
  3. Coroutines ! Kod akışının belirli bir noktada duraklatılıp paraleldeki başka

    bir işin tamamlanmasından sonra devam edebilmesi ! Hafif siklet (lightweight) thread ! Geçtiğimiz hafta stable release olarak yayınlandı
  4. class MainActivity : AppCompatActivity() , CoroutineScope { private lateinit var

    job: Job override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) job = Job() } override fun onDestroy() { super.onDestroy() job.cancel() } }
  5. class MainActivity : AppCompatActivity() , CoroutineScope { private lateinit var

    job: Job override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) job = Job() } override fun onDestroy() { super.onDestroy() job.cancel() } }
  6. class MainActivity : AppCompatActivity() , CoroutineScope { private lateinit var

    job: Job override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) job = Job() } override fun onDestroy() { super.onDestroy() job.cancel() } }
  7. Senaryo #2 iki asenkron işin sonuçlarını birleştirme ! Paralelde çalışan

    iki iş ! Sonuçları birbirine bağımlı ! Sonuç başarılı ya da başarısız tek bir yerde toplanmalı
  8. private fun readFileSingle(name: String): Single<String> { return Single.create<String> { singleEmitter

    -> singleEmitter.onSuccess(readFile(name)) }.subscribeOn(Schedulers.io()) }
  9. Single.zip(readFileSingle("thefile.txt")), readFileSingle(“theotherfile.txt")), BiFunction<String, String, String> { t1, t2 -> "$t1

    \n$t2" }).observeOn(AndroidSchedulers.mainThread()) .subscribe({ textView.text = it }, { e -> if (e is IOException) { handleIOException(e) } Log.e(TAG,e.message,e) })
  10. Single.zip(readFileSingle("thefile.txt")), readFileSingle("theotherfile.txt")), BiFunction<String, String, String> { t1, t2 -> "$t1

    \n$t2" }).observeOn(AndroidSchedulers.mainThread()) .subscribe({ textView.text = it }, { e -> if (e is IOException) { handleIOException(e) } Log.e(TAG,e.message,e) })
  11. Single.zip(readFileSingle("thefile.txt")), readFileSingle("theotherfile.txt")), BiFunction<String, String, String> { t1, t2 -> "$t1

    \n$t2" }).observeOn(AndroidSchedulers.mainThread()) .subscribe({ textView.text = it }, { e -> if (e is IOException) { handleIOException(e) } Log.e(TAG,e.message,e) })
  12. Single.zip(readFileSingle("thefile.txt")), readFileSingle("theotherfile.txt")), BiFunction<String, String, String> { t1, t2 -> "$t1

    \n$t2" }).observeOn(AndroidSchedulers.mainThread()) .subscribe({ textView.text = it }, { e -> if (e is IOException) { handleIOException(e) } Log.e(TAG,e.message,e) })
  13. Single.zip(readFileSingle("thefile.txt")), readFileSingle("theotherfile.txt")), BiFunction<String, String, String> { t1, t2 -> "$t1

    \n$t2" }).observeOn(AndroidSchedulers.mainThread()) .subscribe({ textView.text = it }, { e -> if (e is IOException) { handleIOException(e) } Log.e(TAG,e.message,e) })
  14. launch(Dispatchers.Main) { try { coroutineScope { val s1 = readFileDeferred("thefile.txt").await()

    val s2 = readFileDeferred("theotherfile.txt").await() textView.text = "$s1 \n$s2" } } catch (e: IOException) { handleIOException(e) } }
  15. launch(Dispatchers.Main) { try { coroutineScope { val s1 = readFileDeferred("thefile.txt").await()

    val s2 = readFileDeferred("theotherfile.txt").await() textView.text = "$s1 \n$s2" } } catch (e: IOException) { handleIOException(e) } }
  16. launch(Dispatchers.Main) { try { coroutineScope { val s1 = readFileDeferred("thefile.txt").await()

    val s2 = readFileDeferred("theotherfile.txt").await() textView.text = "$s1 \n$s2" } } catch (e: IOException) { handleIOException(e) } }
  17. Senaryo #3 Asenkron bir api’yi kullanma ! Callback ile çalışan

    bir api’ı belirli bir flowun içerisine katmak istiyoruz ! Elimizde halihazırda bir sonuç varsa sonucu dönüp sonuç yoksa apiden gelecek sonucu döndürmek istiyoruz. ! Kodlama stilinin proje geneli ile aynı olmasını istiyoruz.
  18. private fun getAppFolder(): Single<DriveFolder> { return Single.create<DriveFolder> { emitter ->

    if (folder != null) { emitter.onSuccess(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder emitter.onSuccess(appFolder) }.addOnFailureListener { emitter.onError(it) } } } }
  19. private fun getAppFolder(): Single<DriveFolder> { return Single.create<DriveFolder> { emitter ->

    if (folder != null) { emitter.onSuccess(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder emitter.onSuccess(appFolder) }.addOnFailureListener { emitter.onError(it) } } } }
  20. private fun getAppFolder(): Single<DriveFolder> { return Single.create<DriveFolder> { emitter ->

    if (folder != null) { emitter.onSuccess(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder emitter.onSuccess(appFolder) }.addOnFailureListener { emitter.onError(it) } } } }
  21. private fun getAppFolder(): Single<DriveFolder> { return Single.create<DriveFolder> { emitter ->

    if (folder != null) { emitter.onSuccess(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder emitter.onSuccess(appFolder) }.addOnFailureListener { emitter.onError(it) } } } }
  22. private fun getAppFolder(): Single<DriveFolder> { return Single.create<DriveFolder> { emitter ->

    if (folder != null) { emitter.onSuccess(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder emitter.onSuccess(appFolder) }.addOnFailureListener { emitter.onError(it) } } } }
  23. private fun getAppFolder(): Single<DriveFolder> { return Single.create<DriveFolder> { emitter ->

    if (folder != null) { emitter.onSuccess(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder emitter.onSuccess(appFolder) }.addOnFailureListener { emitter.onError(it) } } } }
  24. getAppFolder().subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ appFolder -> Toast.makeText(this, "AppFolder id'si = ${appFolder.driveId

    ?.resourceId}", Toast.LENGTH_LONG).show() }, { Toast.makeText(this, "Bir hata oluştu", Toast.LENGTH_LONG).show() })
  25. private suspend fun getAppFolder() = suspendCoroutine<DriveFolder>{continuation -> if (folder !=

    null) { continuation.resume(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder continuation.resume(appFolder) }.addOnFailureListener { continuation.resumeWithException(it) } } }
  26. private suspend fun getAppFolder() = suspendCoroutine<DriveFolder>{continuation -> if (folder !=

    null) { continuation.resume(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder continuation.resume(appFolder) }.addOnFailureListener { continuation.resumeWithException(it) } } }
  27. private suspend fun getAppFolder() = suspendCoroutine<DriveFolder>{continuation -> if (folder !=

    null) { continuation.resume(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder continuation.resume(appFolder) }.addOnFailureListener { continuation.resumeWithException(it) } } }
  28. private suspend fun getAppFolder() = suspendCoroutine<DriveFolder>{continuation -> if (folder !=

    null) { continuation.resume(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder continuation.resume(appFolder) }.addOnFailureListener { continuation.resumeWithException(it) } } }
  29. private suspend fun getAppFolder() = suspendCoroutine<DriveFolder>{continuation -> if (folder !=

    null) { continuation.resume(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder continuation.resume(appFolder) }.addOnFailureListener { continuation.resumeWithException(it) } } }
  30. private suspend fun getAppFolder() = suspendCoroutine<DriveFolder>{continuation -> if (folder !=

    null) { continuation.resume(folder !!) } else { getDriveResourceClient().appFolder .addOnSuccessListener { appFolder -> folder = appFolder continuation.resume(appFolder) }.addOnFailureListener { continuation.resumeWithException(it) } } }
  31. launch { try { val appFolder = async(Dispatchers.IO) { getAppFolder()

    }.await() Toast.makeText(this@MainActivity, "AppFolder id'si = ${appFolder.driveId ?.resourceId}", Toast.LENGTH_LONG).show() } catch (e: Exception) { Toast.makeText(this@MainActivity, "Bir hata oluştu", Toast.LENGTH_LONG).show() } }