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 job = launch { | |
// fire and forget task is done here | |
} | |
val deferredJob = async<Int> { | |
// a job which returns some result | |
} | |
// Block until deferredJob returns with the result | |
val result = deferredJob.await() |
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 MyViewModel : ViewModel(), CoroutineScope { | |
override val coroutineContext = | |
viewModelScope.coroutineContext + Dispatchers.Default | |
fun someF() { | |
launch { | |
// No need to write viewModelScope.launch | |
// and this is automatically on Dispatchers.Default | |
} |
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 MyViewModel: ViewModel() { | |
init { | |
viewModelScope.launch { | |
// Coroutine that will be cancelled | |
// when the ViewModel is cleared. | |
} | |
} | |
} |
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
interface SomeApiService { | |
@GET("some_end_point") | |
fun getApiEndpointData(@QueryMap map:Map<String,String>): Call<ApiData> | |
} | |
val service = RetrofitHelper.createRetrofitService(SomeApiService::class.java) | |
// we pass the callback here | |
service.getApiEndpointData().enqueue() |
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
interface SomeApiService { | |
@GET("some_end_point") | |
fun getApiEndpointData(@QueryMap map:Map<String,String>): Call<ApiData> | |
} | |
launch(coroutineExceptionHandler) { | |
val service = RetrofitHelper.createRetrofitService(SomeApiService::class.java) | |
val response = service.getApiEndpointData() | |
// Use the response here | |
} |
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 ceh = CoroutineExceptionHandler { _, throwable -> | |
launch (Dispatchers.Main) { showFailedUI() } | |
} | |
launch(Dispatchers.Default + ceh) { | |
val apiCall1Result = async { | |
val r1 = apiCall1() | |
if(r1.status=="failed") { | |
throw Exception("api call 1 fails") | |
} | |
} |
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 parentJob = scope.launch { | |
val result1 = suspendFunction1() | |
var nonCooperativeChildResult = 0 | |
val nonCooperativeChild = launch { | |
while(isActive) { | |
nonCooperativeChildResult = | |
someRecursiveProcessing(nonCooperativeChildResult) | |
} | |
} | |
val result2 = async { someApiCallWithRetrofit() } |
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 eh = CoroutineExceptionHandler { _, throwable -> | |
println("Oops error occured: ${throwable.localizedMessage}") | |
} | |
launch(eh) { | |
async { | |
delay(100) | |
throw Exception("I failed) | |
} | |
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 eh = CoroutineExceptionHandler { _, throwable -> | |
println("Oops error occured: ${throwable.localizedMessage}") | |
} | |
// Crashes the main thread since top most launch doesn't | |
// handle exception even if child coroutine does | |
launch { | |
delay(100) | |
launch(eh) { | |
throw Exception("I failed") |
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 eh = CoroutineExceptionHandler { _, throwable -> | |
println("Oops error occured: ${throwable.localizedMessage}") | |
} | |
launch(eh) { | |
delay(100) | |
throw Exception("I failed") | |
} | |
Output: | |
Oops error occured: I failed |