Hey everyone Sorry, if this is not the right chann...
# coroutines
v
Hey everyone Sorry, if this is not the right channel to ask this. I'm facing a weird issue with PagingData's
cachedIn()
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
Copy code
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 catch
i
That all sounds WAI - Paging has its own error reporting mechanisms, so
catch
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 end
You'll actually never get any exception from a real
Flow<PagingData>
produced by an actual
Pager
, so what you're testing doesn't actually test any real scenario at all
v
Pager wont ever throw an exception But we can have exceptions in the PagingData transformers, or in some flow operator
My app was crashing because there was an exception thrown inside a
mapLatest {}
operator
i
Yeah, sounds like
catch
before
cachedIn
would be the right tool to avoid crashing the whole app, but tbh, I'd probably just fix your mapLatest
v
Yeah, ideally there should not be any exception at all But if there is for any reason or I intentionally throw some exception,
catch
should catch it
Also, in case of multiple
cachedIn()
, 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()
m
forget the catch. If there is indeed a but in the map operator, the expected result would be to crash. its fine
v
But i dont want app to crash, why would anyone want app to crash I want to show a error UI or toggle a snackbar etc, enabled from the catch operator