https://kotlinlang.org logo
z

Zun

06/04/2021, 12:51 PM
I am using Dropbox Store4 (which is something like a NetworkBoundResource). I am experiencing an issue where my Fetcher is never called because my reader returns a non-null empty list (Store sees this as valid data). How can I adjust my Room DAO to return null if there are no results (so I can successfully trigger Fetch to actually load new data?). See thread for screenshots
DAO
My Store
Only reader is called, Fetcher or Writer is never called
I honestly don't know if this is by design but it returns an empty list but I need a null item
a

André Thiele

06/04/2021, 1:10 PM
What about not using your Dao directly? Can you put some kind of wrapper around it that checks if the list isEmpty and if true, returns null else the list?
z

Zun

06/04/2021, 1:12 PM
I tried out
Copy code
showDao.loadHomeScreen(now).onEmpty { emit(null ) }
with no success
a

André Thiele

06/04/2021, 1:13 PM
can you confirm that onEmpty is called? i think the flow might still emit the empty list and onEmpty is never called
z

Zun

06/04/2021, 1:18 PM
Welp, I tried out a breakpoint and manually logging but it doesn't seem to get called
a

André Thiele

06/04/2021, 1:27 PM
yes, thats why you need to remove the onEmpty callback and put your dao inside something like ShowLocalDataSource(val showDao: ShowDao) { loadHomeScreen() : List<...> { val shows = showDao.loadHomeScreen() return if (shows.isEmpty()) { null else { shows } } }
z

Zun

06/04/2021, 1:30 PM
It works when using List directly but I need to use Flow/LiveData to constantly observe database changes
a

André Thiele

06/04/2021, 1:36 PM
try creating a new flow inside your localdatasource of Flow<List<>?> and then call flow {} in which you decide to emit(shows) or emit(null)
see: Asynchronous Flow | Kotlin (kotlinlang.org)
then do ... = flow { showsDao.loadHomeScreen().collect { shows -> if (shows.isEmpty) { emit(null) } else { emit(shows) } } }
👍 1
z

Zun

06/04/2021, 1:40 PM
that works thank you very much! Still wish
onEmpty
worked because this is quite some lines of code to achieve something simple
a

André Thiele

06/04/2021, 1:41 PM
thats true, great it works. maybe you can shorten this somehow or just include it directly in your store thing but this could get quite messy
11 Views