chi
11/07/2018, 1:51 AMcountries.add(country)
launch(Dispatchers.Main) {
val exp = runExpensiveCode(country)
countries.remove(country)
if (exp != null) doAction()
}
suspend fun runExpensiveCode(country: Country) =
withContext(Dispatchers.Default) {
performVeryExpensiveAction(country) // legacy code used in several parts
}
This still freezes my main screen for some milliseconds, but the Async task implementation doesn't, how can I make this as seamless as possible?mayojava
11/07/2018, 6:09 AMDefault
Dispatcher. How did you check that your main thread is still freezing?chi
11/07/2018, 6:33 AMgildor
11/07/2018, 7:32 AMasync task implementation is much smootherWhat do you actually mean?
gildor
11/07/2018, 7:32 AMgildor
11/07/2018, 7:33 AMgildor
11/07/2018, 7:34 AMgildor
11/07/2018, 7:41 AMgildor
11/07/2018, 7:41 AMgildor
11/07/2018, 7:41 AMgildor
11/07/2018, 7:41 AMAlso, with coroutine, there’s about 5s delay on the main threadWhat does that mean?
gildor
11/07/2018, 7:48 AMgildor
11/07/2018, 7:48 AMgildor
11/07/2018, 7:48 AMgildor
11/07/2018, 7:49 AMgildor
11/07/2018, 7:49 AMgildor
11/07/2018, 7:49 AMgildor
11/07/2018, 7:51 AMgildor
11/07/2018, 7:51 AMgildor
11/07/2018, 7:52 AMgildor
11/07/2018, 7:53 AMrunExpensiveCode
cancellable than it will be possible just cancel task during processing and start new onegildor
11/07/2018, 7:55 AMgildor
11/07/2018, 7:56 AMgildor
11/07/2018, 7:57 AMgildor
11/07/2018, 7:58 AMval bitmapDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
launch(bitmapDispatcher) {
runExpensiveCode()
}
gildor
11/07/2018, 7:59 AMgildor
11/07/2018, 8:01 AMgildor
11/07/2018, 8:02 AMgildor
11/07/2018, 8:03 AMgildor
11/07/2018, 8:03 AMgildor
11/07/2018, 8:04 AMinterrupting the main threadI would profile code to understand what is going on. Maybe you just consumed all the CPU and UI thread doesn’t have resources to render
gildor
11/07/2018, 8:04 AMgildor
11/07/2018, 8:07 AMgildor
11/07/2018, 8:07 AMTolriq
11/07/2018, 8:30 AMTolriq
11/07/2018, 8:42 AMTolriq
11/07/2018, 8:42 AMgildor
11/07/2018, 8:46 AMAnd I forget but async task is 2 to 4 threads so can run up to 4 tasks depending on your device@Tolriq It’s not default AsyncTask behaviour anymore. Starting from Android 3 all the tasks are run on a single BG thread sequentially
gildor
11/07/2018, 8:47 AMgildor
11/07/2018, 8:50 AMgildor
11/07/2018, 8:50 AMgildor
11/07/2018, 8:51 AMasCoroutineDispatcher()
with this executor is goodgildor
11/07/2018, 8:51 AM