Curiosity is a powerful thing. When I saw this RxSwift Driver being discussed on RxSwift Slack and read the helpful RxSwift GitHub issue “Need better explanation / examples for Driver” submitted by Florent Pillet, it seemed helpful to capture what I saw.
As Carlos García shared on the GitHub issue:
One should think of the following:
1 2 3 4 |
let intDriver = sequenceOf(1, 2, 3, 4, 5, 6) .asDriver(onErrorJustReturn: 1) .map { $0 + 1 } .filter { $0 < 5 } |
as the same as:
1 2 3 4 5 6 |
let intObservable = sequenceOf(1, 2, 3, 4, 5, 6) .observeOn(MainScheduler.sharedInstance) .catchErrorJustReturn(1) .map { $0 + 1 } .filter { $0 < 5 } .shareReplay(1) |
Krunoslav Zaher aka kzaher on the RxSwift slack community said that it is a combination of: “main thread + shared subscriptions + no errors.” He goes on to say that shareReplay(1) is a shared subscriptions example. Carlos adds there are several examples in RxExample.
kzaher sums it all up nicely by sharing:
Yeah, it’s just a tiny builder/dsl for Observables. I think this should help me and others a lot with ensuring you can safely bind values to UI.
Using Driver is optional, but it helps the compiler catch things and will help emphasize things that might have been missed. As kzaher put it:
..and warn me when I’ve forgot to handle unexpected errors, or binding from background thread or doing something I should have when dealing with UI applications.
You may want to keep an eye on the RxSwift documentation as it grows and develops over time. Specifically, this Units document. Driver sounds exciting and I look forward to trying it in-depth in the near future!
Jan 2, 2016 Update: The Units document has been updated and much more has been added to it. Good stuff!