Last active
January 2, 2019 12:46
-
-
Save RubyLichtenstein/73b8b26b1a059dd8b8224e45907bd2c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LifeCycle { | |
open class BaseActivity : Activity(), CoroutineScope { | |
private val job = SupervisorJob() | |
override val coroutineContext: CoroutineContext | |
get() = job + Dispatchers.Main | |
override fun onDestroy() { | |
super.onDestroy() | |
job.cancel() | |
} | |
} | |
class MainActivity : BaseActivity() { | |
fun fetchAndShowCoroutines() { | |
launch { | |
val data = withContext(IO) { fetch() } | |
showData(data) | |
} | |
} | |
fun fetchAndShowRx() { | |
fetchSingle() | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.compose(bindToLifeCycle<String, String>()) | |
.subscribe { data: String -> | |
showData(data) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//single flatMap | |
fun flatMapRx(): Single<String> { | |
return fetchSingle() | |
.flatMap { fetchSingle(it) } | |
} | |
suspend fun flatMapCoroutines(): String { | |
val a1 = fetch() | |
return fetch(a1) | |
} | |
//completable flatMap | |
fun flatMapCompletableRx(): Completable { | |
return fetchSingle() | |
.flatMapCompletable { saveRx(it) } | |
} | |
suspend fun flatMapCompletableCoroutines() { | |
val data = fetch() | |
save(data) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment