Observable的創(chuàng)建
常用的為:deferred、of泛烙、just
- empty
//------ empty ------
//MARK: empty
//空序列型宙,只能completed
let emptyOb = Observable<Int>.empty()
_ = emptyOb.subscribe(onNext: { (number) in
print("訂閱:", number)
}, onError: { (error) in
print("error:", error)
}, onCompleted: {
print("completed")
}, onDisposed: {
print("disposed")
})
- just
//------ just ------
//MARK: just
//單個(gè)信號(hào)序列
let array = ["a", "b", "c", "d", "e"]
Observable<[String]>.just(array)
.subscribe { (event) in
print(event)
}.disposed(by: disposeBag)
_ = Observable<[String]>.just(array)
.subscribe(onNext: { (number) in
print("訂閱:", number)
}, onError: { (error) in
print("error:", error)
}, onCompleted: {
print("completed")
}, onDisposed: {
print("disposed")
})
- of
//------ of ------
//MARK: of
//多個(gè)元素
Observable<String>.of("a", "b")
.subscribe { (event) in
print(event)
}.disposed(by: disposeBag)
//字典
Observable<[String: Any]>.of(["name": "aa", "age": 18])
.subscribe { (event) in
print(event)
}.disposed(by: disposeBag)
//數(shù)組
Observable<[String]>.of(["a", "b", "c"])
.subscribe { (event) in
print(event)
}.disposed(by: disposeBag)
- from
//------ from ------
//MARK: from
//從集合中獲取序列匀奏,有可選項(xiàng)
Observable<[String]>.from(optional: ["a", "b"])
.subscribe { (event) in
print(event)
}.disposed(by: disposeBag)
//------ deferred ------
//MARK: deferred
//根據(jù)條件判斷業(yè)務(wù)邏輯蛔翅,根據(jù)業(yè)務(wù)選擇序列
var isOdd = true
_ = Observable<Int>.deferred({ () -> Observable<Int> in
isOdd = !isOdd
if isOdd {
return Observable.of(1, 3, 5, 7, 9)
}
return Observable.of(0, 2, 4, 6, 8)
})
.subscribe({ (event) in
print(event)
})
- range
//------ range ------
//MARK: range
//生成指定范圍內(nèi)的可觀察整數(shù)序列
Observable.range(start: 2, count: 5)
.subscribe { (event) in
print(event)
}.disposed(by: disposeBag)
- generate
//------ generate ------
//MARK: generate
//當(dāng)所有條件都為true時(shí)才給出Observable序列瓶殃,類似數(shù)組遍歷循環(huán)
Observable.generate(initialState: 0, //初始值
condition: { $0 < 10 }, //條件1
iterate: { $0 + 2 }) //條件2
.subscribe { (event) in
print(event)
}.disposed(by: disposeBag)
//數(shù)組遍歷
let arr = ["aa", "bb", "cc", "dd", "ee", "ff"]
Observable.generate(initialState: 0,
condition: { $0 < arr.count },
iterate: { $0 + 1 })
.subscribe { (event) in
print(event)
}.disposed(by: disposeBag)
- timer
//------ timer ------
//MARK: timer
//para1:第一次響應(yīng)距離現(xiàn)在的時(shí)間充包;para2:時(shí)間間隔;para3:線程
Observable<Int>.timer(5, period: 2, scheduler: MainScheduler.instance)
.subscribe { (event) in
print(event)
}
.disposed(by: disposeBag)
//沒有指定period,為一次性執(zhí)行
Observable<Int>.timer(2, scheduler: MainScheduler.instance)
.subscribe { (event) in
print(event)
}
.disposed(by: disposeBag)
- interval
//------ interval ------
//MARK: interval
//從當(dāng)前時(shí)間開始
Observable<Int>.interval(2, scheduler: MainScheduler.instance)
.subscribe { (event) in
print(event)
}
.disposed(by: disposeBag)
- repeatElement
//------ repeatElement ------
//MARK: repeatElement
//創(chuàng)建一個(gè)可以無限發(fā)出給定元素的Event的Observable序列基矮,永不終止
Observable<Int>.repeatElement(5)
.subscribe { (event) in
print(event)
}
.disposed(by: disposeBag)
- error
//------ error ------
//MARK: error
//發(fā)出錯(cuò)誤信號(hào)
Observable<String>.error(NSError.init(domain: "error", code: 1002, userInfo: ["reason": "unknow"]))
.subscribe { (event) in
print(event)
}
.disposed(by: disposeBag)
- never
//------ never ------
//MARK: never
//創(chuàng)建永遠(yuǎn)不會(huì)發(fā)出也不會(huì)終止的Observable序列淆储;
//這種類型的響應(yīng)源,在測試或者組合操作符中禁用確切的源很有用
Observable<String>.never()
.subscribe { (event) in
print(event)
}
.disposed(by: disposeBag)