除非有订阅者,否则Observable不会执行它们的闭包。
创建一个自定义的Observable序列
image.png
let disposeBag = DisposeBag()
let myjust = { (element: String) -> Observable<String> in
return Observable.create{ observer in
observer.on(.next(element))
observer.on(.completed)
return Disposables.create()
}
}
myjust("篮球").subscribe({ (element) in
print(element)
}).disposed(by: disposeBag)
let source = Observable.generate(
initialState: 0,
condition: { $0 < 3 },
iterate: { $0 + 1 }
)
source.subscribe {
print($0)
}
next(0)
next(1)
next(2)
completed
-
deferred
为每一个订阅者创建一个新的Observable
序列。
let disposeBag = DisposeBag()
var count = 1
let defferedSequen = Observable<String>.deferred {
print("count = \(count)")
count += 1
return Observable.create({ (observer) -> Disposable in
observer.onNext("山羊")
observer.onNext("野猪")
observer.onNext("小猫")
return Disposables.create()
})
}
defferedSequen.subscribe(onNext: {
print($0)
}).disposed(by: disposeBag)
defferedSequen.subscribe(onNext: {
print($0)
}).disposed(by: disposeBag)
创建一个只发送completed事件的空Observable序列。
image.png
let disposeBag = DisposeBag()
Observable<Int>.empty().subscribe({ (event) in
print(event)
}).disposed(by: disposeBag)
创建一个永不终止且不发出任何事件的序列。
let disposeBag = DisposeBag()
Observable<String>.never().subscribe({ (_) in
print("this will never be printed")
}).disposed(by: disposeBag)
image.png
- of
创建一个固定数量元素的Observable序列。
let disposeBag = DisposeBag()
Observable.of("元素1","元素2","元素3","元素4","元素5").subscribe(onNext: { (element) in
print(element)
}).disposed(by: disposeBag)
- from
从一个序列(如Array/Dictionary/Set)中创建一个Observable序列。
//从一个序列(如Array/Dictionary/Set)中创建一个Observable序列。
let disposeBag = DisposeBag()
Observable.from(["元素1","元素2","元素3","元素4","元素5"]).subscribe(onNext: {
print($0)
}).disposed(by: disposeBag)
创建一个可观察对象,该对象发出以给定时间间隔为间隔的整数序列
image.png
Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.asyncInstance)
.subscribe{print($0)}
.disposed(by: disposeBag)
// next(0)
// next(1)
// next(2)
// next(3)
// next(4)
创建一个只有一个元素的Observable序列。
image.png
let source = Observable.just(1, 2, 3)
source.subscribe {
print($0)
}
let source2 = Observable.just([1,2,3])
source2.subscribe {
print($0)
}
next((1, 2, 3))
completed
next([1, 2, 3])
completed
创建一个Observable
序列,它会发出一系列连续的整数,然后终止。
let disposeBag = DisposeBag()
Observable.range(start: 1, count: 10).subscribe {
print($0)
}.disposed(by: disposeBag)
创建一个Observable
序列,它可以无限地释放给定元素。
let disposeBag = DisposeBag()
Observable.repeatElement("🏀").take(3).subscribe(onNext: {
print($0)
})
dueTime: 延时
period: 间隔时间
let seq = Observable<Int>.timer(.seconds(2), period: .seconds(1), scheduler: MainScheduler.asyncInstance)
print("订阅 time:", Date())
seq.subscribe{print("收到事件:",$0,"time:",Date())}
.disposed(by: disposeBag)
订阅 time: 2018-03-23 03:06:49 +0000
收到事件: next(0) time: 2018-03-23 03:06:51 +0000
收到事件: next(1) time: 2018-03-23 03:06:52 +0000
收到事件: next(2) time: 2018-03-23 03:06:53 +0000
收到事件: next(3) time: 2018-03-23 03:06:54 +0000
收到事件: next(4) time: 2018-03-23 03:06:55 +0000
收到事件: next(5) time: 2018-03-23 03:06:56 +0000
收到事件: next(6) time: 2018-03-23 03:06:57 +0000
............
创建一个不会发送任何条目并且立即终止错误的Observable序列。
let disposeBag = DisposeBag()
Observable<Int>.error(NSError()).subscribe { (err) in
print(err)
}.disposed(by: disposeBag)
网友评论