I'm sorting a large list (10.000+) items with use ...
# coroutines
r
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:
message has been deleted
or B:
message has been deleted
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
B is better because it defers concurrency to the caller and uses the correct dispatcher.
Don't think it changes anything.
r
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
Why you are not using a query + flow in room?
r
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
Ah, didn't notice it was in the view model. I guess A is fine then.
w
As a side note, shouldn’t you use
Dispatchers.Default
for sorting a large list? Sorting is cpu-bound
r
yes
đź‘Ť 1