1

I want to implement a simple login feature using ReactiveSwift.

Here is my code for ViewController:

func setupRac() {
    viewmodel = SignInViewModel()

    self.viewmodel.username <~ self.usernameTextField.reactive.continuousTextValues
    self.viewmodel.password <~ self.passwordTextField.reactive.continuousTextValues

    self.loginButton.reactive.pressed = CocoaAction(viewmodel.submit) { sender in
        return (self.usernameTextField.text, self.passwordTextField.text)
    }

The ViewModel:

class SignInViewModel {
let username = MutableProperty<String?>(nil)
let password = MutableProperty<String?>(nil)

let usernameValid: Property<Bool>
let passwordValid: Property<Bool>
let valid: Property<Bool>
let submit: Action<(String?, String?), String, NetworkError>

init() {
    self.usernameValid = username.map {
        return ($0 ?? "").characters.count > 0
    }

    self.passwordValid = password.map {
        return ($0 ?? "").characters.count > 0
    }

    self.valid = Property.combineLatest(self.usernameValid, self.passwordValid).map({ (usernameValid, passwordValid) in
        return usernameValid && passwordValid
    })

    self.submit = Action(enabledIf: self.valid, { username, password in
        return MembershipManager.sharedInstance.signin(username: username!, password: password!)
    })
}

When I put breakpoint to the line return MembershipManager.sharedInstance.signin(..., it's just hit one time when I press the login button only. After that, when I press the button again, nothing happen.

Does anyone know the reason why? I just started with ReactiveSwift only.

t4nhpt
  • 5,264
  • 4
  • 34
  • 43
  • 2
    When an action is executing, you can't start a new action. Are you sure that the `MembershipManager.sharedInstance.signin()` completes before you press the button again? – gkaimakas Jul 05 '17 at 07:19
  • I'm not really sure about the "complete" concept in Reactive. I have a wrapper class above the Alamofire, and it returns a SignalProduct (another guy wrote this code, but I cannot contact him). Anyway, I'm sure that the request has been executed, and completed. – t4nhpt Jul 05 '17 at 08:28
  • is this code available somewhere to take a look at it? – gkaimakas Jul 05 '17 at 09:34
  • Sorry, I don't have the source here. I will update my question tonight. Thank you. – t4nhpt Jul 05 '17 at 09:47
  • You can use the [`logEvents`](https://github.com/ReactiveCocoa/ReactiveSwift/blob/master/Documentation/DebuggingTechniques.md#debugging-event-streams) operator to see the events sent on `MembershipManager.sharedInstance.signin` to check if there is a `.completed` event after signin. – MeXx Jul 09 '17 at 11:28

0 Answers0