aiidziis
06/04/2020, 9:52 AMclass DbHelper(db: Db) {
suspend fun insert(elements: List<Element>) = withContext(Dispatchers.Default) {
db.elementQueries.transaction {
db.elementQueries.insert(.....)
}
}
}
This works fine when elements list is not too big, but now we encountered use case where we need to insert larger amount of data (in worst case insert is 1200ms), and now this same approach is causing problems on iOS because this all happens on Main Thread(async) , and while the data is inserting UI is getting blocked when user interacts with UI (scrolling).
Is there a way with current coroutines versions "1.3.5-native-mt"
to improve this? afaik with current coroutines version it is not possible move insert to Background Thread?louiscad
06/04/2020, 11:21 AMDispatchers.Default
be on a background thread.aiidziis
06/04/2020, 11:29 AMinternal class EngineScope : CoroutineScope {
internal val job = SupervisorJob()
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
}
and I am calling this insert function:
engineScope.launch { dbHelper.insert(elements) }
So this shouldn’t block main thread?louiscad
06/04/2020, 11:32 AMNSThread.isMainThread
at the right place to find out.
(Needs import of import platform.Foundation.NSThread
) You might need to expect/actual it if in common code.aiidziis
06/04/2020, 11:41 AMinsert
function is not blocking UI. Will investigate further what else might be blocking it. Thanks.Kris Wong
06/04/2020, 1:00 PMaiidziis
06/04/2020, 1:02 PMDispatchers.Default
.flowOn(Dispatchers.Default)
on Flow and thought that it would make select
on Dispatchers.Default
.