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

how to make bidirectional binding between view and viewmodel use RxSwift #606

Closed
pengxianhe opened this Issue Apr 7, 2016 · 5 comments

Comments

Projects
None yet
5 participants
@pengxianhe
Copy link

pengxianhe commented Apr 7, 2016

I know use bindto to bind viewmodel.property and view ,but i did not find
bidirectional binding method

@kzaher

This comment has been minimized.

Copy link
Member

kzaher commented Apr 9, 2016

Hi @pengxianhe ,

please take a look at example app. You can find example of how to do two way binding there.

infix operator <-> {
}

func <-> <T>(property: ControlProperty<T>, variable: Variable<T>) -> Disposable {
    let bindToUIDisposable = variable.asObservable()
        .bindTo(property)
    let bindToVariable = property
        .subscribe(onNext: { n in
            variable.value = n
        }, onCompleted:  {
            bindToUIDisposable.dispose()
        })

    return StableCompositeDisposable.create(bindToUIDisposable, bindToVariable)
}
         let sliderValue = Variable<Float>(1.0)
        slider.rx_value <-> sliderValue

        sliderValue.asObservable()
            .subscribeNext { [weak self] x in
                self?.debug("UISlider value \(x)")
            }
            .addDisposableTo(disposeBag)
@pengxianhe

This comment has been minimized.

Copy link
Author

pengxianhe commented Apr 12, 2016

OK, I got it

@pengxianhe pengxianhe closed this Apr 12, 2016

@riccardo-tesio

This comment has been minimized.

Copy link

riccardo-tesio commented Jul 16, 2016

IMHO this is not a perfect solution. It is elegant and it works, but each event is returned to the sender.
I mean:
property -> updates the variable -> updates the property
and
variable -> updates the property -> updates the variable

I am trying with this implementation: in my app it works, but I am new to Rx and there could be other anomalies:

func <-> <T>(property: ControlProperty<T>, variable: Variable<T>) -> Disposable {

    var updating = false

    let bindToUIDisposable = variable.asObservable().filter({ _ in
        updating = !updating
        return updating
    }).bindTo(property)
    let bindToVariable = property.filter({ _ in
        updating = !updating
        return updating
    }).subscribe(onNext: { n in
        variable.value = n
        }, onCompleted:  {
            bindToUIDisposable.dispose()
    })

    return StableCompositeDisposable.create(bindToUIDisposable, bindToVariable)
}
@Davarg

This comment has been minimized.

Copy link

Davarg commented Sep 15, 2018

@anirudhamahale

This comment has been minimized.

Copy link

anirudhamahale commented Feb 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment