使用 withLatestFrom 收集存储变量 属性
Collecting stored variable property using withLatestFrom
我想知道 RxSwift 中是否有一种方法可以观察存储变量的值 属性。例如。在以下示例中:
var updatedValue: Int = 0
var observedValue: Observable<Int> {
return Observable.create({ (observer) -> Disposable in
observer.onNext(updatedValue)
return Disposables.create()
})
}
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
updatedValue = updatedValue + 1;
}
let myObservable = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
.publish()
myObservable.connect()
myObservable
.withLatestFrom(observedValue)
.subscribe { (event) in
print(event)
}
我们有变量 属性 updatedValue 和热可观察变量 myObservable。我们还在 Timer.scheduledTimer....
中增加 updatedValue 的值
这里的流程非常简单。当我们订阅时,observedValue 被调用,我们从 observedValue 获得 onNext,然后是 Disposables.create()。然后我们打印事件 onNext(0)。
因为myObservable是基于Observable.interval,同样的withLatestFrom值每秒都会在onNext中打印出来。
问题:有没有办法在每次 myObservable 发出新事件时打印 updatedValue 的最后一个值?所以不是 0,0,0... 我们得到 0,1,2...
我知道 updatedValue 可以声明为 BehaviorRelay。
我也知道我们可以使用 .map { } 来捕获 self.updatedValue.
但我想知道是否有任何方法可以围绕标准变量 属性 创建一个 Observable 包装器,以便它在每次触发序列发送事件时使用最新值调用 onNext?无需捕获自我或更改 updatedValue 上的声明。
感谢任何意见和想法!
But I'm wondering if there is any way to create a Observable wrapper around standard variable property so it calls onNext with most recent value every time trigger sequence sends an event? Without capturing self or changing declaration on updatedValue.
正确答案是否定的。如果不涉及自我,就无法对 updatedValue
做任何事情。一种方法是使用 Observable<Int>.interval(1, scheduler: MainScheduler.instance).compactMap { [weak self] _ in self?.updatedValue }.distinctUntilChanged()
(您对 publish 和 connect 的使用很奇怪且不必要,)但这涉及到 self.
因为你的 属性 是一个值类型,访问它的唯一方法是通过 self,即使根本不涉及 Rx。
RxCocoa 有一个方便的 KVO 包装器。您应该能够在 NSObject 子类的 .rx
扩展中使用它。
对于你的问题,我想你可以这样做:
let updatedValueObservable = self.rx.observe(Int.self, "updatedValue")
我想知道 RxSwift 中是否有一种方法可以观察存储变量的值 属性。例如。在以下示例中:
var updatedValue: Int = 0
var observedValue: Observable<Int> {
return Observable.create({ (observer) -> Disposable in
observer.onNext(updatedValue)
return Disposables.create()
})
}
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
updatedValue = updatedValue + 1;
}
let myObservable = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
.publish()
myObservable.connect()
myObservable
.withLatestFrom(observedValue)
.subscribe { (event) in
print(event)
}
我们有变量 属性 updatedValue 和热可观察变量 myObservable。我们还在 Timer.scheduledTimer....
中增加 updatedValue 的值这里的流程非常简单。当我们订阅时,observedValue 被调用,我们从 observedValue 获得 onNext,然后是 Disposables.create()。然后我们打印事件 onNext(0)。
因为myObservable是基于Observable.interval,同样的withLatestFrom值每秒都会在onNext中打印出来。
问题:有没有办法在每次 myObservable 发出新事件时打印 updatedValue 的最后一个值?所以不是 0,0,0... 我们得到 0,1,2...
我知道 updatedValue 可以声明为 BehaviorRelay。 我也知道我们可以使用 .map { } 来捕获 self.updatedValue.
但我想知道是否有任何方法可以围绕标准变量 属性 创建一个 Observable 包装器,以便它在每次触发序列发送事件时使用最新值调用 onNext?无需捕获自我或更改 updatedValue 上的声明。
感谢任何意见和想法!
But I'm wondering if there is any way to create a Observable wrapper around standard variable property so it calls onNext with most recent value every time trigger sequence sends an event? Without capturing self or changing declaration on updatedValue.
正确答案是否定的。如果不涉及自我,就无法对 updatedValue
做任何事情。一种方法是使用 Observable<Int>.interval(1, scheduler: MainScheduler.instance).compactMap { [weak self] _ in self?.updatedValue }.distinctUntilChanged()
(您对 publish 和 connect 的使用很奇怪且不必要,)但这涉及到 self.
因为你的 属性 是一个值类型,访问它的唯一方法是通过 self,即使根本不涉及 Rx。
RxCocoa 有一个方便的 KVO 包装器。您应该能够在 NSObject 子类的 .rx
扩展中使用它。
对于你的问题,我想你可以这样做:
let updatedValueObservable = self.rx.observe(Int.self, "updatedValue")