Recommended way to pass user input to ViewModel with RAC3

125 Views Asked by At

Colin Eberhardt has a great article on how to do some bindings in reactive-cocoa 3. There was however, one solution I didn't really like, and it had to do with his text field. He had created a property in his ViewModel for "searchtext" which he was observing in his view model, and he bound it to the text field's text. I'm not a fan of this because the view model shouldn't, in my mind, be holding onto this text, nor should anyone else be able to observe that text other than the view model. To me this should be accomplished by either a Signal or Signal Producer.

So my question is what the recommended way is to pass this data from, let's say a UITextView to the view model to act on it. I have 2 ways so far to accomplish it:

  • ViewModel

    var signalProducer: SignalProducer<String, NSError>? {
        didSet {
            if let signalProducer = signalProducer {
                signalProducer
                    ... do stuff
            }
        }
    }
    
  • ViewController

    viewModel.signalProducer = textView.rac_textSignal().toSignalProducer()
        |> map { text in text as! String }
    

(should my view model have direct access to the signal producer?)


Or

  • ViewModel

    let (textViewTextSignal, textViewTextSink) = Signal<String, NoError>.pipe()
    
    init() {
        textViewTextSignal
            ... do stuff with it
    }
    
  • ViewController

    textView.rac_textSignal().toSignalProducer()
        |> map { text in text as! String }
        |> start(next: { [unowned self] text in
            sendNext(self.viewModel.textViewTextSink, text)
        })
    

(should any object be able to trigger this signal?)


I may be missing some fundamental concepts between Signal and Signal Producer here also, I'm just wondering how other people have been accomplishing this interaction.

0

There are 0 best solutions below