https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
m

Mark Lurie

02/15/2023, 3:23 PM
Hey everyone, Quick question: I’m new to using StateFlow, i got an data class object that holds a list of other data class,
Copy code
private val _invoiceIntroFlowState: MutableStateFlow<InvoiceIntroFlowState> = MutableStateFlow(InvoiceIntroFlowState(customizationData = InvoiceCustomizationResponseEntity()))
val invoiceIntroFlowState: StateFlow<InvoiceIntroFlowState> = _invoiceIntroFlowState.asStateFlow()
Copy code
data class InvoiceIntroFlowState(
    val uiStatus: UiStatus = UiStatus.NONE,
    val error: Failure? = null,
    val customizationData: InvoiceCustomizationResponseEntity
)
Any attempt to do any kind of deep-copy that i know doesn’t work for me to trigger the new state event:
Copy code
_invoiceIntroFlowState.update { currentState ->
                currentState.copy(
                    customizationData = currentState.customizationData.copy(
                        logos = another //updatedLogos.map { it.copy() }
                    )
                )
            }
Any help will be highly appreciated
u

ubuntudroid

02/15/2023, 8:14 PM
Can you show what
InvoiceCustomizationResponseEntity
looks like? Could it have a custom equals implementation that omits logos? And, as
logos
seems to be a Collection and Collections in most cases are considered equal if their items are equal (and you are doing
.map { it.copy() }
which doesn’t really change the contents from an equality perspective),
logos
seems to be equal to
another
. I’d suggest to debug into
MutableStateFlow.update()
and look especially close at the call to
compareAndSet()
. I guess that it always returns
true
for you right at the first star, and as
prevValue
and
nextValue
are equal, it is not changing the underlying value which in turn causes the flow to stay silent. `compareAndSet()`’s doc says:
This function use a regular comparison using Any.equals. If both expect and update are equal to the current value, this function returns true, but it does not actually change the reference that is stored in the value.
19 Views