Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RubyLichtenstein/73b8b26b1a059dd8b8224e45907bd2c8 to your computer and use it in GitHub Desktop.
Save RubyLichtenstein/73b8b26b1a059dd8b8224e45907bd2c8 to your computer and use it in GitHub Desktop.
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)
}
}
}
}
//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