Home > Net >  is it ok to use value field in coroutines?
is it ok to use value field in coroutines?

Time:07-25

I have a flow that contains the user's info(as a single object). there are some operations, that need to access the mentioned flow, in a single-shot op. no observing, just reading the required info once.

I know there are single() and other functions that can do the thing, but my question is, is it ok to use value property of the flow instead of those functions?

the flow is a StateFlow so value is immutable and I want to access the field only in the view model so I'm not violating encapsulation.

CodePudding user response:

is it ok to use value property of the flow instead of those functions?

Yes, it's ok to do so. The purpose of a StateFlow is to expose a "current value" at all times, so it's a valid use case to want to access .value directly, especially for one-shot read-only operations.

It doesn't even violate encapsulation.

  • Related