序列的中的序列 - string - 映射 - 序列
,而且發起屢次訂閱
,以下代碼://建立序列
let result = inputTF.rx.text.skip(1)
.flatMap { [weak self](input) -> Observable<Any> in
return (self?.dealwithData(inputText:input ?? ""))!
}
//連續訂閱兩次
result.subscribe(onNext: { (element) in
print("訂閱到了--:\(element)")
})
result.subscribe(onNext: { (element) in
print("訂閱到了--:\(element)---\(Thread.current)")
})
//模擬網絡請求
func dealwithData(inputText:String)-> Observable<Any>{
print("請求網絡了 \(Thread.current)") // data
return Observable<Any>.create({ (ob) -> Disposable in
if inputText == "1234" {
ob.onError(NSError.init(domain: "com.smart_M.cn", code: 10086, userInfo: nil))
}// json - model username pasword
// username passopp - lgError - 8001 - httpCoder 100 - 500
// 封裝 vm - error -
DispatchQueue.global().async {
print("發送以前看看: \(Thread.current)")
ob.onNext("已經輸入:\(inputText)")
ob.onCompleted()
}
return Disposables.create()
})
複製代碼
運行代碼,並在inputTF
輸入內容1
,打印以下:json
請求網絡了 <NSThread: 0x600000063b40>{number = 1, name = main}
發送以前看看: <NSThread: 0x604000269600>{number = 4, name = (null)}
請求網絡了 <NSThread: 0x600000063b40>{number = 1, name = main}
訂閱到了--:已經輸入:1
發送以前看看: <NSThread: 0x604000463c80>{number = 5, name = (null)}
訂閱到了--:已經輸入:1---<NSThread: 0x604000463c80>{number = 5, name = (null)}
複製代碼
輸入內容1234
, 打印error
以下:bash
請求網絡了 <NSThread: 0x600000063b40>{number = 1, name = main}
Unhandled error happened: Error Domain=com.lgcooci.cn Code=10086 "(null)"
subscription called from:
發送以前看看: <NSThread: 0x604000462040>{number = 7, name = (null)}
請求網絡了 <NSThread: 0x600000063b40>{number = 1, name = main}
Unhandled error happened: Error Domain=com.lgcooci.cn Code=10086 "(null)"
subscription called from:
發送以前看看: <NSThread: 0x6000004798c0>{number = 6, name = (null)}
複製代碼
從上面的打印能夠看出:網絡
浪費網絡
子線程
解決方法:app
.share(replay: 1, scope: .whileConnected)
複製代碼
.observeOn(MainScheduler())
複製代碼
3.檢測錯誤事件(不會反悔error,致使崩潰)dom
.catchErrorJustReturn("檢測到了錯誤事件")
複製代碼
let result = inputTF.rx.text.skip(1)
.flatMap { [weak self](input) -> Observable<Any> in
return (self?.dealwithData(inputText:input ?? ""))!
.observeOn(MainScheduler())
.catchErrorJustReturn("檢測到了錯誤事件")
}
.share(replay: 1, scope: .whileConnected)
複製代碼
----------輸入1:
請求網絡了 <NSThread: 0x604000078d00>{number = 1, name = main}
發送以前看看: <NSThread: 0x604000473200>{number = 5, name = (null)}
訂閱到了--:已經輸入:1
訂閱到了--:已經輸入:1---<NSThread: 0x604000078d00>{number = 1, name = main}
----------輸入1234:
請求網絡了 <NSThread: 0x604000078d00>{number = 1, name = main}
訂閱到了--:檢測到了錯誤事件
訂閱到了--:檢測到了錯誤事件---<NSThread: 0x604000078d00>{number = 1, name = main}
發送以前看看: <NSThread: 0x60000066d2c0>{number = 6, name = (null)}
複製代碼
上面的問題雖然解決了,可是:async
Driver
出場了//observale -> driver
let result = inputTF.rx.text.orEmpty
.asDriver()
.flatMap {
return self.dealwithData(inputText: $0)
.asDriver(onErrorJustReturn: "檢測到了錯誤事件")
}
// 綁定到了UI - 主線程
// titlt - 非error
result.map { "長度: \(($0 as! String).count)"}.drive(self.textLabel.rx.text)
result.map { "\($0 as! String)"}
.drive(self.btn.rx.title())
複製代碼
執行代碼打印以下:ui
asDriver
是ControlProperty
的擴展方法,返回一個Driver
對象spa
extension ControlProperty {
public func asDriver() -> Driver<Element> {
return self.asDriver { _ -> Driver<Element> in
#if DEBUG
rxFatalError("Somehow driver received error from a source that shouldn't fail.")
#else
return Driver.empty()
#endif
}
}
}
複製代碼
Driver
又是SharedSequence
的序列別名線程
public typealias Driver<Element> = SharedSequence<DriverSharingStrategy, Element>
複製代碼
source.share(replay: 1, scope: .whileConnected)
public struct DriverSharingStrategy: SharingStrategyProtocol {
public static var scheduler: SchedulerType { return SharingScheduler.make() }
public static func share<Element>(_ source: Observable<Element>) -> Observable<Element> {
return source.share(replay: 1, scope: .whileConnected)
}
}
複製代碼
SharingScheduler
內部make()
方法public enum SharingScheduler {
/// Default scheduler used in SharedSequence based traits.
public private(set) static var make: () -> SchedulerType = { MainScheduler() }
}
複製代碼
ObservableConvertibleType
的擴展方法public func asDriver(onErrorJustReturn: Element) -> Driver<Element>
中:
observeOn(DriverSharingStrategy.scheduler
進行線程調度catchErrorJustReturn(onErrorJustReturn)
錯誤處理
extension ObservableConvertibleType {
...
public func asDriver(onErrorJustReturn: Element) -> Driver<Element> {
let source = self
.asObservable()
.observeOn(DriverSharingStrategy.scheduler)
.catchErrorJustReturn(onErrorJustReturn)
return Driver(source)
}
...
}
複製代碼