Would it be a good approach to use runBlocking in following implementation? Would it cause UI to be laggy?
I use it all in my viewModel.
lateinit var centralReturnsPaging: Flow<PagingData<ClaimsAndReturnsList>>
Retrieving data from DataStore
private suspend fun getPharmacyId(): Int = dataSource.userData.first().ckk.toInt()
Assigning centralReturnsPaging and invoking runBlocking on Dispatcher.IO thread
private fun observePagingData() {
centralReturnsPaging = pagingUseCase.getClaimsAndReturns.invoke(
size = 15,
pharmacyId = runBlocking(dispatcherIO) { getPharmacyId() },
emptyList = { determineIfEmpty(it) },
claimsAndReturnsList = { claimsAndReturnsLists, page ->
assignList(
claimsAndReturnsLists
)
}
).cachedIn(viewModelScope)
}
Combining
runBlocking
with Dispatchers.IO is not beneficial. It causes two threads to be occupied instead of only one. In this case, there isn't any blocking work anyway (assuming whatever is under the hood of your Flow suspends when appropriate) so it doesn't hurt, but it's unnecessary.Using
runBlocking
always has the potential to make the app laggy/stuttery. If you're just retrieving some preferences from a DataStore, it probably won't be noticeable. If you're doing a query of a big database, then it's not advisable.