This file contains 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
// dispatches execution onto the Android main UI thread | |
private val uiContext: CoroutineContext = UI | |
// represents a common pool of shared threads as the coroutine dispatcher | |
private val bgContext: CoroutineContext = CommonPool |
This file contains 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
private fun loadData() = launch(uiContext) { | |
view.showLoading() // ui thread | |
try { | |
val task = async(bgContext) { dataProvider.loadData("Task") } | |
val result = task.await() // non ui thread, suspend until task is finished | |
view.showData(result) // ui thread | |
} catch (e: RuntimeException) { | |
e.printStackTrace() |
This file contains 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 MainPresenter(private val view: MainView, | |
private val dataProvider: DataProviderAPI | |
private val uiContext: CoroutineContext = UI, | |
private val ioContext: CoroutineContext = CommonPool) { | |
private fun loadData() = launch(uiContext) { // use the provided uiContext (UI) | |
view.showLoading() | |
// use the provided ioContext (CommonPool) | |
val task = async(bgContext) { dataProvider.loadData("Task") } |
This file contains 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
val exceptionHandler: CoroutineContext = CoroutineExceptionHandler { _, throwable -> throwable.printStackTrace() } | |
private fun loadData() = launch(uiContext + exceptionHandler) { | |
view.showLoading() // ui thread | |
val task = async(bgContext) { dataProvider.loadData("Task") } | |
val result = task.await() // non ui thread, suspend until the task is finished | |
view.showData(result) // ui thread | |
} |
This file contains 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
var job: Job? = null | |
fun startPresenting() { | |
job = loadData() | |
} | |
fun stopPresenting() { | |
job?.cancel() | |
} | |
This file contains 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
private fun loadData() = async(uiContext) { | |
view.showLoading() // ui thread | |
val task = async(bgContext) { dataProvider.loadData("Task") } | |
val result = task.await() // non ui thread, suspend until the task is finished | |
view.showData(result) // ui thread | |
} |
This file contains 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
data class Result<out T>(val success: T? = null, val error: Throwable? = null) | |
private fun loadData() = launch(uiContext) { | |
view.showLoading() // ui thread | |
val task = async(bgContext) { dataProvider.loadData("Task") } | |
val result: Result<String> = task.await() // non ui thread, suspend until the task is finished | |
if (result.success != null) { | |
view.showData(result.success) // ui thread |
This file contains 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
private fun loadData() = launch(uiContext) { | |
view.showLoading() // ui thread | |
val task = async(bgContext) { dataProvider.loadData("Task") } | |
// non ui thread, suspend until the task is finished or return null in 2 sec | |
val result = withTimeoutOrNull(2, TimeUnit.SECONDS) { task.await() } | |
view.showData(result) // ui thread | |
} |
This file contains 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
private fun loadData() = launch(uiContext) { | |
view.showLoading() // ui thread | |
val task1 = async(bgContext) { dataProvider.loadData("Task 1") } | |
val task2 = async(bgContext) { dataProvider.loadData("Task 2") } | |
val result = "${task1.await()} ${task2.await()}" // non ui thread, suspend until finished | |
view.showData(result) // ui thread | |
} |
This file contains 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 MainPresenter(private val view: MainView, | |
private val dataProvider: DataProviderAPI) { | |
private fun loadData() = launch(UI) { // UI - dispatches execution onto Android main UI thread | |
view.showLoading() | |
// CommonPool - represents common pool of shared threads as coroutine dispatcher | |
val task = async(CommonPool) { dataProvider.loadData("Task") } | |
val result = task.await() | |