Messing around with Room for the first time, and w...
# coroutines
b
Messing around with Room for the first time, and wondering what you all think about this usage:
Copy code
viewModelScope.launch {
customerRepo.customer(customerUid) // Returns a `Flow<Customer>`
  .combine(controllerRepo.controllersForCustomer(customerUid)) { customer, controllers ->
    CustomerWithControllers(customer, controllers)
  }
  .collect { customerWithControllers ->
    internalState.value = State.Loaded(customerWithControllers)
  }
}
This works just fine, but when I go to delete a
customer
, the deletion cascades to the
controllers
and what you end up with is a new
CustomerWithControllers
with empty controllers because the
customer
Flow
last emission was the customer that was just deleted. I solved this by holding onto the
Job
returned by
launch
, and when I go delete a customer, I basically cancel that
Job
, delete the customer, and then execute a function that checks for the existence of any customer and if one doesn't exist, brings the user to a FTUX screen. Is this a decent way of handling this? It'd be nice if
Room
had some way to emit an
Empty
token or something to indicate the table is empty ­čĄö
Posted this here since it's sorta a "best practice coroutine usage" question versus a Room one (since I'm fairly certain there's no
Empty
token in Room)... Another way of asking would be "is
combine
a good idea here?"
In a different Slack group it was recommended my
Flow
return a nullable type, so that may be the direction I have to go!
r
Hmm can't you just do filter a , for only valid CustomerWithControllers ? Or perhaps a custom channelFlow, that only calls
Copy code
controllerRepo.controllersForCustomer(customerUid))? upon a valid return of customerRepo.customer(customerUid)
@Brandon Trautmann (aka Mr. fakesOverMocks ­čśů