RxSwift - 为什么将 ControlProperty 特性包装到驱动程序中?
RxSwift - Why wrap ControlProperty trait into a Driver?
在官方的RxSwift documentation中,描述了traits Driver
和 ControlProperty
之间有很多相似之处(不会出错,observe occurs on main scheduler, share and重播副作用),但同时在提供的示例中 ControlProperty
rx.text
被包装到驱动程序中。
所以问题是:
- 将
ControlProperty
包装成 Driver
特征是否有任何真正的优势?
- 如果
ControlProperty
和 Driver
都应该默认共享和重播,为什么 .share(replay: 1)
运算符在第一个代码中被调用而在第二个代码中没有被调用?
这里附上文档中的参考代码:
发件人:
let results = query.rx.text
.throttle(0.3, scheduler: MainScheduler.instance)
.flatMapLatest { query in
fetchAutoCompleteItems(query)
.observeOn(MainScheduler.instance) // results are returned on MainScheduler
.catchErrorJustReturn([]) // in the worst case, errors are handled
}
.share(replay: 1) // HTTP requests are shared and results replayed
// to all UI elements
results
.map { "\([=10=].count)" }
.bind(to: resultCount.rx.text)
.disposed(by: disposeBag)
results
.bind(to: resultsTableView.rx.items(cellIdentifier: "Cell")) { (_, result, cell) in
cell.textLabel?.text = "\(result)"
}
.disposed(by: disposeBag)
收件人:
let results = query.rx.text.asDriver() // This converts a normal sequence into a `Driver` sequence.
.throttle(0.3, scheduler: MainScheduler.instance)
.flatMapLatest { query in
fetchAutoCompleteItems(query)
.asDriver(onErrorJustReturn: []) // Builder just needs info about what to return in case of error.
}
results
.map { "\([=11=].count)" }
.drive(resultCount.rx.text) // If there is a `drive` method available instead of `bind(to:)`,
.disposed(by: disposeBag) // that means that the compiler has proven that all properties
// are satisfied.
results
.drive(resultsTableView.rx.items(cellIdentifier: "Cell")) { (_, result, cell) in
cell.textLabel?.text = "\(result)"
}
.disposed(by: disposeBag)
谢谢并致以最诚挚的问候!
在第一个示例中,throttle
使用了 returns Observable。
在第二个示例中,由于 asDriver()
调用,使用了不同的 throttle
returns 驱动程序(即 SharedSequence<DriverSharingStrategy, String>
)
您的困惑可能源于 RxSwift 库中有两个 throttle
函数。一个是 ObservableType 的扩展(ControlProperty 是 ObservableType 的扩展),而另一个是 SharedSequenceConvertibleType
的扩展(Driver 是它的扩展。)
在官方的RxSwift documentation中,描述了traits Driver
和 ControlProperty
之间有很多相似之处(不会出错,observe occurs on main scheduler, share and重播副作用),但同时在提供的示例中 ControlProperty
rx.text
被包装到驱动程序中。
所以问题是:
- 将
ControlProperty
包装成Driver
特征是否有任何真正的优势? - 如果
ControlProperty
和Driver
都应该默认共享和重播,为什么.share(replay: 1)
运算符在第一个代码中被调用而在第二个代码中没有被调用?
这里附上文档中的参考代码:
发件人:
let results = query.rx.text
.throttle(0.3, scheduler: MainScheduler.instance)
.flatMapLatest { query in
fetchAutoCompleteItems(query)
.observeOn(MainScheduler.instance) // results are returned on MainScheduler
.catchErrorJustReturn([]) // in the worst case, errors are handled
}
.share(replay: 1) // HTTP requests are shared and results replayed
// to all UI elements
results
.map { "\([=10=].count)" }
.bind(to: resultCount.rx.text)
.disposed(by: disposeBag)
results
.bind(to: resultsTableView.rx.items(cellIdentifier: "Cell")) { (_, result, cell) in
cell.textLabel?.text = "\(result)"
}
.disposed(by: disposeBag)
收件人:
let results = query.rx.text.asDriver() // This converts a normal sequence into a `Driver` sequence.
.throttle(0.3, scheduler: MainScheduler.instance)
.flatMapLatest { query in
fetchAutoCompleteItems(query)
.asDriver(onErrorJustReturn: []) // Builder just needs info about what to return in case of error.
}
results
.map { "\([=11=].count)" }
.drive(resultCount.rx.text) // If there is a `drive` method available instead of `bind(to:)`,
.disposed(by: disposeBag) // that means that the compiler has proven that all properties
// are satisfied.
results
.drive(resultsTableView.rx.items(cellIdentifier: "Cell")) { (_, result, cell) in
cell.textLabel?.text = "\(result)"
}
.disposed(by: disposeBag)
谢谢并致以最诚挚的问候!
在第一个示例中,throttle
使用了 returns Observable。
在第二个示例中,由于 asDriver()
调用,使用了不同的 throttle
returns 驱动程序(即 SharedSequence<DriverSharingStrategy, String>
)
您的困惑可能源于 RxSwift 库中有两个 throttle
函数。一个是 ObservableType 的扩展(ControlProperty 是 ObservableType 的扩展),而另一个是 SharedSequenceConvertibleType
的扩展(Driver 是它的扩展。)