Hi all :wave: — I’m trying to build a synchroniser with Jetpack’s WorkManager for sync’ing records c...
l
Hi all 👋 — I’m trying to build a synchroniser with Jetpack’s WorkManager for sync’ing records created locally/saved in Room with a remote server, when the internet connection is available. Room represents my “unsyncronised” records as a
Flow<List<LocalRecord>>
, instead of a
List<LocalRecord>
, and if I try to
collect
on the flow it blocks the worker. Is this a good idea? If so, how do I retrieve the most up-to-date list of unsync’d `LocalRecord`’s from the flow, without blocking the worker? I’ve tried
collectLatest
and
last
, both of which don’t behave as expected. If this isn’t a good idea, would it be better to spawn one worker for each record, at the risk of potentially overloading my server if a large number (200+) of records need syncing? I’ve gone through the nowinandroid implementation, and it’s doing some particularly fruity stuff with a
DelegatingWorker
I don’t really understand 😵‍💫
c
Then just use a suspend function instead of one returning a flow. You can then use a
CoroutineWorker
to have a scope for your suspending calls. https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker
l
so I can return just a
List<LocalRecord>
from my Room DAO then, instead of a Flow?
Yes, I can just return a
List<LocalRecord>
and I was making my life harder for myself with the
Flow
😅 😅 😅 Thank you Christian! 🙇