flatMap从未调用过 - ReactiveCocoa

时间:2016-06-14 17:44:32

标签: swift2 reactive-programming reactive-cocoa

美好的一天, 我对flatMap的问题从未调用过。

我有两种方法。 First(getApiUser)获取令牌,第二个(登录)用户登录。登录方法需要来自getApiUser方法的令牌。

GetApiUser()效果很好。调用http请求并接收令牌。

func getApiUser() -> SignalProducer<ApiUser, NoError>{

    return SignalProducer<ApiUser, NoError> {[weak self] sink, disposable in
        let parameters = [
            "login" : self!.loginName,
            "password" : self!.password
        ]

        Alamofire.request(.POST, self!.url, parameters: parameters)
            .responseJSON { response in
                if let JSON = response.result.value {
                    if let apiUser : ApiUser = decode(JSON) {
                        sink.sendNext(apiUser)
                        sink.sendCompleted()
                    }
                }
        }  
    }
}

登录使用getApiUser()来接收令牌:

func login(login : String, password : String) -> SignalProducer<Driver, NoError>{
    let driver = _authorizator
        .getApiUser()
        .map{
            $0.token
        }
        .flatMap(FlattenStrategy.Latest){token in
            self.loginDriver(login, password: password, token: token)
        }

    return driver
}

//TODO: This method never called
private func loginDriver(login : String, password : String, token : String) -> SignalProducer<Driver, NoError>{

    return SignalProducer<Driver, NoError> {[weak self] sink, dis in
        let parameters = [
            "syslogin" : login,
            "syspassword" : password
        ]

        let headers = [
            "ExsysAuthenticateToken" : token
        ]

        Alamofire.request(.POST, self!._loginDriverUrl, parameters: parameters, headers: headers).responseJSON { response in
            if let JSON = response.result.value{
                if let driver : Driver = decode(JSON){
                    sink.sendNext(driver)
                    sink.sendCompleted()
                }
            }
        }

    }

}

我的错误在哪里? :( Thnx寻求帮助!

0 个答案:

没有答案