Vaibhav Jaiswal
10/24/2024, 8:44 AMcachedIn()
flow operator
• If I have a cachedIn()
operator before catch {}
operator, catch operator does not work and the flow throws an exception and my app crashes
• If i have catch{}
before cachedIn()
, the flow never completes, and nothing is displayed in my UI
It all works fine, if I dont have cachedIn()
at all
This is a small test I've written to verify it
test("Should not crash"){
val flow = flow<PagingData<Int>> { throw IllegalStateException("Testing") }
shouldNotThrowAny {
flow.cachedIn(this).catch { }.collect()
}
}
It passes if I remove cachedIn()
, but fails with it
and test never completes if I put cachedIn()
after catchIan Lake
10/24/2024, 1:30 PMcatch
after cachedIn
is never going to fire and cachedIn
transforms your Flow into essentially a State flow, which never terminates, so it is expected that the collect
would never endIan Lake
10/24/2024, 1:32 PMFlow<PagingData>
produced by an actual Pager
, so what you're testing doesn't actually test any real scenario at allVaibhav Jaiswal
10/24/2024, 1:33 PMVaibhav Jaiswal
10/24/2024, 1:34 PMmapLatest {}
operatorIan Lake
10/24/2024, 1:45 PMcatch
before cachedIn
would be the right tool to avoid crashing the whole app, but tbh, I'd probably just fix your mapLatestVaibhav Jaiswal
10/24/2024, 1:50 PMcatch
should catch itVaibhav Jaiswal
10/24/2024, 1:56 PMcachedIn()
, if I have multiple .combine(anotherFlow)
and cachedIn()
before each (as app crashes if I dont add cachedIn)
How can i add a catch
before, it will always be after some cachedIn()
myanmarking
10/25/2024, 2:30 PMVaibhav Jaiswal
10/25/2024, 2:31 PM