thread, work-manager, process, async
Co+Routine - Co means Coopearion, Routine means Function. when functions cooperate with each other
fun functionA(case: Int) {
when (case) {
1 -> {
taskA1()
functionB(1)
}
2 -> {
taskA2()
functionB(2)
}
3 -> {
taskA3()
functionB(3)
}
4 -> {
taskA4()
functionB(4)
}
}
}
Example: viewModelScope - MVP vs MVVM
view.lifecycleScope.launch {
val moderationApi = view.lifecycleScope.async { view.moderationApi(question) }
repository?.prompt(x, object :
InterfaceCallbackGeneral<BaseResponse<Prompt>> {
override fun onSuccess(r: BaseResponse<Prompt>, message: String?) {
lifecycleScope.launch {
val flagged = moderationApi.await()
r.data?.let { view.callChatBotApi(question,questionMethod,flagged,it) }
}
}
override fun onNetworkError(message: String,exceptionMessage: String) {}
})
}
Scope | Defination |
---|---|
GlobalScope | lifetime of application |
CoroutineScope | UI components |
ViewModelScopre | specific type of CoroutineScope tied to the lifecycle of a ViewModel (performing background tasks, data retrieval, or asynchronous operations within a ViewModel) |
LifecycleScope | Activity or Fragment |
Dispatchers help coroutines in deciding the thread on which the work has to be done
Dispatcher | Defination |
---|---|
Dispatchers.Default | This dispatcher is optimized for CPU-intensive work (sorting, searching list in memory) |
Dispatchers.Main | This dispatcher is designed for UI-related tasks in Android applications. |
Dispatchers.IO | such as network or database operations. |
Dispatchers.Unconfined | This dispatcher runs the coroutine in the caller thread until the first suspension point. After suspension, it resumes execution in the appropriate thread, which might be different from the original caller thread. |