Reputation: 767
how do I map this
func save() -> SignalProducer<Void, NetworkError>
to
var saveAction: Action<AnyObject, Bool, NoError>
i'm a bit confused with the syntax
service.save()
.observeOn(QueueScheduler.mainQueueScheduler)
.map<Bool>( _ in true) // how to map void to bool
.flatMapError {
error in
// how to map to a NoError?
}
also, what should be the best practice in using actions? should the NetworkError bubble up to the controller so it can display the error in a Popup dialog?
Upvotes: 1
Views: 1265
Reputation: 25907
You ask 3 things, so let's go one by one:
- Going from a
Void
toBool
Assuming you have a foo: SignalProducer<Void, Error>
:
let bar: SignalProducer<Bool, Error> = foo.map { _ in true}
- Going from a
NetworkError
to aNoError
This is not intuitive, but you can make use of Swift's type inference and do something like this:
let bar: SignalProducer<Void, NoError> = foo.flatMapError { _ in SignalProducer.empty }
Your func save() -> SignalProducer<Void, NetworkError>
could then become:
let save: SignalProducer<Void, NetworkError> = ...
let newSave: SignalProducer<Bool, NoError> = save.map {_ in true}.flatMapError { _ in SignalProducer.empty }
- should the NetworkError bubble up to the controller so it can display the error in a Popup dialog?
Eventually you have to convert the error into something readable. The Controller (assuming we are talking about the UIViewController
), could make use of a secondary entity to translate this error, into a string or a pair of strings (title + body). If you are using MVVM, the ViewModel, would that transformation.
Upvotes: 3