最近开始看RxSwift ,将自己的学习过程记录下来。
在ReactiveCocoa
中,所有的数据都是以signal流的形式传递,其实在Rx
中也是如此,本质并没有改变,改变的是形式。ReactiveCocoa 中的signal 对应Rx 的Observable
- 创建信号
- 常见方法的使用
常见方法的使用
RxCocoa 中的UI类
number: UITextField! 无需通过添加任何操作,只要subscribeNext 操作就会执行打印输出
number1.rx_text.subscribeNext{ (text) -> Void in
print(" --- text \(text)");
}
combinLatest(...)
将多个信号整合成一个信号,任何一个信号的改变均会触发操作
Observable.combineLatest(number1.rx_text, number2.rx_text, number3.rx_text) { textValue1, textValue2, textValue3 -> Int in
return (Int(textValue1) ?? 0) + (Int(textValue2) ?? 0) + (Int(textValue3) ?? 0)
}
.map {
print("value = \($0)");
return $0.description // $0 代表第一个参数
}
.bindTo(result.rx_text)
.addDisposableTo(disposeBag)
map
信号映射转换。例如 将一个String 转换为 Int
bindTo
值绑定,将一个信号与另外一个信号绑定
常见方法
create
func createSequence(){
let myCreate = { (element: Int) -> Observable in
return Observable.create { observer in
observer.on(.Next(element))
observer.on(.Completed)
return NopDisposable.instance
}
}
let subscription5 = myCreate(1).subscribe { event in
print(event)
/*
Next(1)
Completed
*/
}
}
of
func ofSequence(){
let sequenceElements = Observable.of(1, 2, 3, 4)
let _ = sequenceElements.subscribe { event in
print(event)
}
/*
Next(1)
Next(2)
Next(3)
Next(4)
Completed
*/
}
deferred
func deferSquence(){
let deferredSequence: Observable = Observable.deferred {
print("creating")
return Observable.create { observer in
print("emmiting")
observer.on(.Next(0))
observer.on(.Next(1))
observer.on(.Next(2))
return NopDisposable.instance
}
}
_ = deferredSequence
.subscribe { event in
print(event)
}
_ = deferredSequence
.subscribe { event in
print(event)
}
}
Subject 既可以作为Observer, 也可以作为Observable,作为observer,可以订阅一个或者多个observables,作为observable,可以传递其观察的item
PublishSubject
只有在subscribe之后才会接收输出结果,如果subscribe之后没有数据的发送,则也不会有输出。这里的on
如同ReactiveCocoa 的Signal.sendNext()方法
func publishSubject(){
//let disposeBag = DisposeBag()
let subject = PublishSubject()
subject.on(.Next("0"))
subject.subscribe { event in
print("1->\(event)")
}
subject.on(.Next("a"))
subject.on(.Next("b"))
subject.subscribe { event in
print("2->\(event)")
}
subject.subscribeNext { (vc) -> Void in
print("3->\(vc)")
}
subject.on(.Next("c"))
subject.on(.Next("d"))
subject.addDisposableTo(DisposeBag());
/*
不会缓存数据
1->Next(a)
1->Next(b)
1->Next(c)
2->Next(c)
3 c
1->Next(d)
2->Next(d)
3 d
*/
}
replaySubject
每次subscribe 之后会先输出缓冲的数据,可以先发送数据然后再subscribe。(这与ReactiveCocoa 的replaySubject 原理应该是一样的,都是有数据缓冲池,所有发送过的数据都不会消失)。 可以设置缓冲大小。
func replaySubject(){
let disposeBag = DisposeBag()
// 会缓存数据
let subject = ReplaySubject.create(bufferSize: 1)// 设置缓冲的大小
subject.on(.Next("0"))
subject.subscribeNext { (vc) -> Void in
print("1->\(vc)")
}
subject.on(.Next("a"))
subject.on(.Next("b"))
subject.subscribeNext { (vc) -> Void in
print("2->\(vc)")// 当subscribeNext 后,会先输出缓冲的数据
}
subject.on(.Next("c"))
subject.on(.Next("d"))
subject.addDisposableTo(disposeBag);
/*
1->0
1->a
1->b
2->b // 缓冲为1 所以输出最新的一个
1->c
2->c
1->d
2->d
*/
}
BehaviorSubject
When an observer subscribes to a BehaviorSubject, it begins by emitting the item most recently emitted by the source Observable (or a seed/default value if none has yet been emitted) and then continues to emit any other items emitted later by the source Observable(s).
func behavirSubject(){
let subject = BehaviorSubject(value: "z")
subject.on(.Next("0"))
subject.on(.Next("1"))
subject.subscribe { event in
print("1->\(event)")
}
subject.on(.Next("a"))
subject.on(.Next("b"))
subject.subscribe { event in
print("2->\(event)")
}
subject.on(.Next("c"))
subject.on(.Completed)
/*
1->Next(z)// 如果没有默认值 则使用初始化的默认值,如果已经在subscribe 之前设置,则使用最新的
1->Next(a)
1->Next(b)
2->Next(b)
1->Next(c)
2->Next(c)
1->Completed
2->Completed
*/
}
Variable
Variable wraps BehaviorSubject. Advantage of using variable over BehaviorSubject is that variable can never explicitly complete or error out, and BehaviorSubject can in case Error or Completed message is send to it. Variable will also automatically complete in case it's being deallocated.
func varibaleSequence(){
let disposeBag = DisposeBag()
let variable = Variable("z")
variable.asObservable().subscribeNext { (vc) -> Void in
print("1->\(vc) ")
}
// writeSequenceToConsole("1", sequence: variable.asObservable()).addDisposableTo(disposeBag)
variable.value = "a"
variable.value = "b"
variable.asObservable().subscribeNext { (vc) -> Void in
print("2->\(vc) ")
}
// writeSequenceToConsole("2", sequence: variable.asObservable()).addDisposableTo(disposeBag)
variable.value = "c"
variable.value = "d"
/*
Varibale 是BehaviorSubject 的包装器,较于BehaviorSubject 不会显示输出 complete 或 error
Variable 在释放后会自动 complete
1->z
1->a
1->b
2->b
1->c
2->c
1->d
2->d
*/
}