Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS Livedata wrong observers call order #159

Closed
Dorofeev opened this issue Oct 21, 2021 · 1 comment
Closed

iOS Livedata wrong observers call order #159

Dorofeev opened this issue Oct 21, 2021 · 1 comment
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Dorofeev
Copy link
Contributor

code sample

val test = MutableLiveData("")

    fun bind1() {
        test.addObserver {
            println("first $it")

            if (it == "A") {
                test.value = "B"
            }
        }
    }

    fun bind2() {
        test.addObserver {
            println("second $it")
        }
    }

    fun start() {
        bind1()
        bind2()
        test.value = "A"
    }

After we call start function on iOS, in console we will see that observers calls in wrong order:

first A
first B
second B
second A

It happened because if we change value inside observer, changeValue function called recursively.
For workaround we can call observers with storedValue, as below

    protected fun changeValue(value: T) {
        storedValue = value

        observers.forEach { it(storedValue) }
    }
@Alex009 Alex009 added the bug Something isn't working label Oct 22, 2021
@Alex009 Alex009 added this to the 0.12.0 milestone Oct 22, 2021
@Alex009 Alex009 self-assigned this Feb 25, 2022
@Alex009
Copy link
Member

Alex009 commented Feb 25, 2022

will be available in 0.12.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants