RACCommand混乱

时间:2015-04-08 14:58:50

标签: ios swift reactive-cocoa

我正在尝试在我的PasswordResetViewController上使用RACCommand。在我的viewDidLoad中,我有以下内容:

sendButtonItem = UIBarButtonItem(title: "Reset Password", style: .Done, target: self, action: nil)

var emailIsValid = textField.rac_textSignal().map { text -> AnyObject! in
    return count((text as! String)) > 0
}

let passwordResetSignal = RACSignal.createSignal { (subscriber) -> RACDisposable! in
    User.requestPasswordResetForEmailInBackground(self.textField.text) { success, error in
        if success {
            subscriber.sendNext(success)
            subscriber.sendCompleted()
        } else {
            subscriber.sendError(error)
        }
    }

    return nil
}

sendButtonItem.rac_command = RACCommand(enabled: emailIsValid) { (input) in
    return passwordResetSignal
}

sendButtonItem.rac_command.executionSignals.subscribeError { (error) -> Void in
    println("ERROR!")
}

sendButtonItem.rac_command.executionSignals.subscribeNext { (success) -> Void in
    println("SUCCESS!")
}

navigationItem.rightBarButtonItem = sendButtonItem

rightBarButtonItem按照我的预期启用/禁用,但我没有收到任何下一个或错误事件。

1 个答案:

答案 0 :(得分:1)

executionSignals是信号的信号。换句话说:每次执行命令时,它都会发送命令的信号(在您的情况下为passwordResetSignal)作为其下一个值。

如果您要订阅passwordResetSignal发送的值,请使用switchToLatest

sendButtonItem.rac_command.executionSignals.switchToLatest().subscribeNext { (success) -> Void in
    println("SUCCESS!")
}

errors中有一个单独的信号RACCommand,可用于订阅从执行信号收到的错误(注意:错误以next值发送):

sendButtonItem.rac_command.errors.subscribeNext { (error) -> Void in
    println("ERROR!")
}