Reactivecocoa: Handling errors with CocoaAction

Created on 13 Mar 2017  路  2Comments  路  Source: ReactiveCocoa/ReactiveCocoa

I was building an app using MVVM, in my bindViewModel method, I have the following code:

signInButton.reactive.pressed = viewModel.executeSignIn

where executeSignIn is a type of CocoaAction<UIButton>.

How can I handle the errors, e.g., incorrect username or password.

I know thatCocoaAction wraps an Action, and Action has a property errors that can let us observe values, so I came up with an idea that I can pass the closure (Error) -> () to the view model initializer, therefore, I can handle errors like the following code:

let signInAction = ...

signInAction.errors.observeValues(errorClosure)

self.executeSignIn = CocoaAction<UIButton>(signInAction) {}

But it seems clumsy, what if I have a lots of CocoaActions, in that way, I have to pass so many error closures!!!

Is there a elegant way? Please help.

question

Most helpful comment

A bit of off-topic, but AFAIK in MVVM, the VM should not know anything about the view (e.g. that certain action should be triggered by UIButton).

All 2 comments

It depends on what you want to do with your errors. You could also do something like this:

errorTextField.text <~ signInAction.errors.map { ... }

A bit of off-topic, but AFAIK in MVVM, the VM should not know anything about the view (e.g. that certain action should be triggered by UIButton).

Was this page helpful?
0 / 5 - 0 ratings