Pablo
01/12/2021, 4:17 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>)
but what if I have some mappers and some business logic? Is it safe to remove the withContext(<http://Dispatchers.IO|Dispatchers.IO>)
?Nicholas Doglio
01/12/2021, 5:05 PMAnyone of you can explain how retrofit / room are doing the main-safety stuff?Under the hood both Retrofit and Room are delegating to their own way of handling async calls. For Room see this article: https://medium.com/androiddevelopers/room-coroutines-422b786dc4c5 For Retrofit the
suspend
calls just delegate to the built in Call<T>.enqueue
which uses it’s own background executor. see https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/KotlinExtensions.kt
I know that if I put suspend I can avoid theIf the business logic and mappers are exclusively working with Room and Retrofit calls then yes it’s fine to remove them.but what if I have some mappers and some business logic? Is it safe to remove thewithContext(<http://Dispatchers.IO|Dispatchers.IO>)
?withContext(<http://Dispatchers.IO|Dispatchers.IO>)
louiscad
01/12/2021, 6:19 PMDispatchers.Default
because it can consume a bunch of CPU (as all allocation can).Pablo
01/13/2021, 7:56 AM<http://Dispatchers.IO|Dispatchers.IO>
unnecessary I guess because retrofit has it's own stuff, but when I'm getting the DTO I'm doing some mappers and sort etc, but it's super fast, do I have to use Dispatchers.Default
anyways?.... I thought I had to change the Dispatcher when I have some heavy task that could work the UIlouiscad
01/13/2021, 8:43 AM<http://Dispatchers.IO|Dispatchers.IO>
to Dispatchers.Default
without going through the main thread in between, there's no actual thread switching, so it's worth doing any computation/allocation/mapping in Dispatchers.Default
by default, an switch to <http://Dispatchers.IO|Dispatchers.IO>
when needed.Pablo
01/13/2021, 9:11 AMMainScope()
so, once I have that I do something like
scope.launch{
//UI stuff
withContext(IO) { usecase call }
//UI stuff
}
The thing is my usecase calls my data source and datasource does a retrofit call, everything is suspend stuff, but the problem is once I have the DTO on datasource I do a mapper to convert it to domain, then in the use-case perhaps I sort or I'm doing something (not always...)
For these cases do I have to change the dispatcher ?
I mean I want to remove IO because I do not need it to call the retrofit call but under the hood I have more "logic" is not like get the data parsed from JSON and print it, no, I'm doing more stuff..louiscad
01/13/2021, 9:12 AMDispatchers.Default { }
Pablo
01/13/2021, 9:17 AMDispatchers.Main
ok, but then if we have stuff like mappers/sortings/etc better on Dispatchers.Default
louiscad
01/13/2021, 9:18 AMPablo
01/13/2021, 9:18 AMDispatchers.Default
, right instead?louiscad
01/13/2021, 9:18 AMPablo
01/13/2021, 9:25 AMlouiscad
01/13/2021, 9:25 AMDispatchers.Default