Skip to content

Instantly share code, notes, and snippets.

@AndreyArapov
Created May 24, 2022 08:49
Show Gist options
  • Select an option

  • Save AndreyArapov/cbcfdd605b53dcff4ad00d3791d90795 to your computer and use it in GitHub Desktop.

Select an option

Save AndreyArapov/cbcfdd605b53dcff4ad00d3791d90795 to your computer and use it in GitHub Desktop.
class View {
fun show(s: String) { println(s) }
}
// Coroutines
class Presenter1(
private val context: CoroutineContext
) {
private val scope = CoroutineScope(SupervisorJob() + context)
fun attach(v: View) {
scope.launch {
v.show(loadStringFromApi())
}
}
fun detach() {
scope.cancel()
}
private suspend loadStringFromApi(): String { /* ... long loading ... */ }
}
class Presenter2(
private val scope: CoroutineScope
) {
private var job: Job? = null
fun attach(v: View) {
job = scope.launch {
v.show(loadStringFromApi())
}
}
fun detach() {
job?.cancel()
}
private suspend loadStringFromApi(): String { /* ... long loading ... */ }
}
// RxJava
class RxPresenter {
private val disposable = CompositeDisposable()
fun attach(v: View) {
disposable += loadStringFromApi().subscribe { v.show(it) }
}
fun detach() {
disposable.clear()
}
private loadStringFromApi(): Single<String> { /* ... long loading ... */ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment