https://kotlinlang.org logo
Title
r

Remy Benza

02/07/2021, 10:05 AM
I'm sorting a large list (10.000+) items with use of the
<http://Dispatcher.IO|Dispatcher.IO>
from an Android room database. What pattern is recommended to use?
A:
or B:
with A calling
filterNotificationsOnDateRange()
directly from the view (Fragment)
with B I'm calling it inside
lifecycleScope.launchWhenCreate { }
in the Fragment
would this change if I where to call another suspend function inside
filterNotificationsOnDateRange()
? Let's say a call to get different data from the room db
d

Dominaezzz

02/07/2021, 10:15 AM
B is better because it defers concurrency to the caller and uses the correct dispatcher.
Don't think it changes anything.
r

Remy Benza

02/07/2021, 10:16 AM
But shouldn't the view be as 'dumb' as possible and the viewmodel handle all the logic?
if you go for option A the scope will be canceled when the fragment lifecycle ends, aka on a config change. If you use the viewmodelscope the filter result would still be available
j

Javier

02/07/2021, 10:34 AM
Why you are not using a query + flow in room?
r

Remy Benza

02/07/2021, 10:34 AM
because I want to pull in the large data set once the sceens opens, then I let the user filter on various conditions
I dont want this dataset to be updated automatically while the screen is open, with use of Flow
d

Dominaezzz

02/07/2021, 10:58 AM
Ah, didn't notice it was in the view model. I guess A is fine then.
w

wasyl

02/08/2021, 7:46 AM
As a side note, shouldn’t you use
Dispatchers.Default
for sorting a large list? Sorting is cpu-bound
r

Remy Benza

02/08/2021, 7:46 AM
yes
👍 1