1

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?

CodeBender
  • 35,668
  • 12
  • 125
  • 132
Ant
  • 747
  • 1
  • 8
  • 19

1 Answers1

3

You ask 3 things, so let's go one by one:

  1. Going from a Void to Bool

Assuming you have a foo: SignalProducer<Void, Error>:

let bar: SignalProducer<Bool, Error> = foo.map { _ in true}
  1. Going from a NetworkError to a NoError

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 }
  1. 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.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137