Any idea why this code is not returning? It keeps ...
# coroutines
a
Any idea why this code is not returning? It keeps suspended forever
Copy code
userRepository.streamUser().filterLoggedIn().firstOrNull()
The repository returns a Logged Out user, but I'm only interested in logged in users, I would expect that since this flow becomes an empty flow, then
firstOrNull
should return null, right or am I missing something?
z
filterLoggedIn()
doesn’t emit anything, and doesn’t complete, so
firstOrNull()
won’t emit IIRC
a
Copy code
fun Flow<UserResult>.filterLoggedIn(): Flow<UserResult.LoggedIn> =
    filterIsInstance()
That's the implementation of it, it is just a mere
filterIsInstance
o
it would rely on
streamUser()
completing, otherwise how would it know if it's empty?
👍 1
a
Yeah, you are right, the key is on the completion of the flow. I changed the code to
Copy code
userRepository.streamUser().take(1).filterLoggedIn().firstOrNull()
and it is working as expected, thanks everyone.
z
userRepository.streamUser().firstOrNull { .. }
you can use a custom predicate IIRC
o
as I understand that would have the same issue
z
oh, yeah
🤦