Skip to content

Instantly share code, notes, and snippets.

View dmytrodanylyk's full-sized avatar

Dmytro Danylyk dmytrodanylyk

View GitHub Profile
// 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
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()
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") }
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
}
var job: Job? = null
fun startPresenting() {
job = loadData()
}
fun stopPresenting() {
job?.cancel()
}
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
}
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
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
}
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
}
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()