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)}
当属性改变时,它将通知变量,并设置变量的值,当设置变量的值时,它将通知属性.我认为这将导致无休止的循环……
解决方法 感谢提出问题,我花了一些时间来挖掘ControlProperty实现(注意我已经添加了一个.deBUG()调用来跟踪为控件属性生成的值).public struct ControlProperty<PropertyType> : ControlPropertyType { public typealias E = PropertyType let _values: Observable<PropertyType> let _valueSink: AnyObserver<PropertyType> public init<V: ObservableType,S: ObserverType where E == V.E,E == S.E>(values: V,valueSink: S) { _values = values.deBUG("Control property values").subscribeOn(ConcurrentMainScheduler.instance) _valueSink = valueSink.asObserver() } public func on(event: Event<E>) { switch event { case .Error(let error): bindingErrorToInterface(error) case .Next: _valueSink.on(event) case .Completed: _valueSink.on(event) } }}
我的测试设置如下,我删除了所有定位在这里以缩短它:
import UIKitimport RxSwiftimport RxCocoaclass VIEwController: UIVIEwController { let variable = Variable<Bool>(false); let bag = disposeBag(); overrIDe func loadVIEw() { super.loadVIEw() let aSwitch = UISwitch(); vIEw.addSubvIEw(aSwitch) (aSwitch.rx_value <-> variable).adddisposableto(bag); let button = UIbutton(); button.rx_tap.subscribeNext { [weak self] in self?.variable.value = true; }.adddisposableto(bag) vIEw.addSubvIEw(button); } }infix operator <-> {}func <-> <T>(property: ControlProperty<T>,variable: Variable<T>) -> disposable{ let bindToUIdisposable = variable.asObservable().deBUG("Variable values in bind") .bindTo(property) let bindToVariable = property .deBUG("Property values in bind") .subscribe(onNext: { n in variable.value = n },onCompleted: { bindToUIdisposable.dispose() }) return StableCompositedisposable.create(bindToUIdisposable,bindToVariable) }
现在结果.首先我们尝试点击按钮,该按钮应该将变量设置为true.这将在ControlProperty上触发(事件:事件< E>)并将开关值设置为true.
2016-05-28 12:24:33.229: Variable values in bind -> Event Next(true)// value flowvalue assigned to Variable -> Variable emits event -> ControlProperty receives event -> value assigned to underlying control property (e.g. `on` for `UISwitch`)
接下来让我们触发开关.正如我们所看到的,控件生成一个事件,作为UIControlEventValueChanged的结果,它通过ControlProperty上的_values传递,然后将其值分配给Variable值,如上例所示.但是没有循环,因为更新到Variable值不会触发交换机上的控制事件.
2016-05-28 12:29:01.957: Control property values -> Event Next(false)2016-05-28 12:29:01.957: Property values in bind -> Event Next(false)2016-05-28 12:29:01.958: Variable values in bind -> Event Next(false)// value flowtrigger the state of control (e.g. `UISwitch`) -> ControlProperty emits event -> value assigned to Variable -> Variable emits event -> ControlProperty receives event -> value assigned to underlying control property (e.g. `on` for `UISwitch`)
所以一个简单的解释是:
>一旦触发某种UIControlEvent,就会发出控件的值
>当值直接分配给控件属性时,控件不会触发更改事件,因此没有循环.
希望它有所帮助,抱歉有点凌乱的解释 – 我通过实验找到了它)
总结以上是内存溢出为你收集整理的ios – RxSwift中的双向绑定全部内容,希望文章能够帮你解决ios – RxSwift中的双向绑定所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)