Created
May 24, 2022 08:49
-
-
Save AndreyArapov/cbcfdd605b53dcff4ad00d3791d90795 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 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