1
votes

I am using flow in my app however I am having some issues with it hitting the logic twice with distinct or 8/9 times without it. I am using livedata as flow with databinding.

xml

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/amount_edit"
                isFocused="@={viewModel.amountFocused}"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberSigned"
                android:maxLines="1"
                android:text="@={viewModel.amount}" />

code

private val amountFlow = amount.asFlow().mapLatest {
    Timber.d("FLOW : amountFlow ${it}")
    if (it != null && it.length >= MIN_QUERY_LENGTH) {
        it.toIntOrNull()
    } else {
        null
    }
}.distinctUntilChanged()

this debug statement is called twice using distinctUntilChanged() or 8-9 times without it. I am not really sure why but this flow is being combined with another flow and its calling my other logic way to much.

1

1 Answers

0
votes

Solved this a few days ago on my own. Turns out the live data deeper down in my code where I convert a combine flow to live data a few times for different things is causing the issue. by making all the flows rerun.