问题
I'm the first time to ask a question,I'm learning RxSwift, how to use bind to and driver, what's the difference between of driver and bind to.Anyone else learning RxSwift now.If you are learning RxSwift or Swift or OC,i hope we can be friends and learn from each other.
回答1:
@iwillnot response is fine but I will try to improve it with an example:
Imagine you have this code:
let intObservable = sequenceOf(1, 2, 3, 4, 5, 6)
.observeOn(MainScheduler.sharedInstance)
.catchErrorJustReturn(1)
.map { $0 + 1 }
.filter { $0 < 5 }
.shareReplay(1)
As @iwillnot wrote:
Driver You can read more in detail what the Driver is all about from the documentation. In summary, it simply allows you to rely on these properties: - Can't error out - Observe on main scheduler - Sharing side effects
if you use Driver
, you won't have to specify observeOn
, shareReplay
nor catchErrorJustReturn
.
In summary, the code above is similar to this one using Driver
:
let intDriver = sequenceOf(1, 2, 3, 4, 5, 6)
.asDriver(onErrorJustReturn: 1)
.map { $0 + 1 }
.filter { $0 < 5 }
More details
回答2:
I suggest that you read the documentation for RxSwift Traits such as Driver.
Why use Traits?
Swift has a powerful type system that can be used to improve the correctness and stability of applications and make using Rx a more intuitive and straightforward experience.
By using units that follow constraints, we can rely on the compiler to show us errors if we're trying to do something we're not supposed to do (e.g. perform UI code on a Driver's drive
method).
It is the same concept as to why we use data structures like Stacks and Queues depending on what is appropriate for the context or problem.
Driver
You can read more in detail what the Driver is all about from the documentation. In summary, it simply allows you to rely on these properties:
- Can't error out
- Observe on main scheduler
- Sharing side effects
which is common when dealing with your user interface.
Why it's named Driver
Its intended use case was to model sequences that drive your application.
Community
Come join the RxSwift community in Slack if you are interested to meet like-minded and new and old RxSwift-ers. :)
- Quotations are lifted from the documentation of RxSwift.
来源:https://stackoverflow.com/questions/43097102/use-rxswift-driver-and-bind-to