我正在学习RxSwift,我遇到了登录逻辑问题。
我的代码运行正常吗?退出后一次,部分第二次。
这是来自我的viewmodel:
func transform(input: Input) -> Output {
let user = fetchUser(loginAction: input.loginAction, domain: input.domain,
username: input.username, password: input.password)
return Output(user: user, error: errorTracker.asDriver())
}
func fetchUser(loginAction: Driver<Void>, domain: Driver<String>, email: Driver<String>, password: Driver<String>)->Driver<User>{
let credentials = Driver.combineLatest(domain, email, password) {
$0
}
return loginAction.withLatestFrom(credentials)
.flatMapLatest { [unowned self] (domain, username, password) in
return self.useCase.login(params: ["domain": domain, "username": username, "password": password])
.trackActivity(self.activityIndicator)
.trackError(self.errorTracker)
.asDriverOnErrorJustComplete()
.map { [unowned self] in
if let token = $0.token {
return self.decodeUserId(token: token)
}
return ""
}
.flatMapLatest { [unowned self] userId in
return self.useCase.getUser(params: ["userId": userId])
.trackActivity(self.activityIndicator)
.trackError(self.errorTracker)
.asDriverOnErrorJustComplete()
}
}
}
struct Input {
let loginAction: Driver<Void>
let tenant: Driver<String>
let email: Driver<String>
let password: Driver<String>
}
struct Output {
let user: Driver<User>
let error: Driver<Error>
}
它首次运行,登录检索包含令牌字符串的对象,在映射部分中解码以获取用户ID,并且getUser通过id获取用户。
在视图控制器中,我有这种绑定方法:
func bindViewModel() {
let domainChange = domainField.rx.text.orEmpty.asDriver()
let usernameChange = usernameField.rx.text.orEmpty.asDriver()
let passwordChange = passwordField.rx.text.orEmpty.asDriver()
let input = LoginViewModel.Input(loginAction: loginButton.rx.tap.asDriver(),
domain: domainChange,
username: usernameChange,
password: passwordChange)
let output = viewModel.transform(input: input)
output.user.drive(successBinding).addDisposableTo(disposeBag)
output.error.drive(errorBinding).addDisposableTo(disposeBag)
}
successBinding和errorBinding都是UIBindingObservers,我认为它们不是问题所在。 注销(返回登录,弹出)后,当我尝试点击loginButton时,只运行第一部分,登录方法并成功检索一个令牌,但它不会转到映射逻辑或用户获取逻辑。 任何帮助将不胜感激。
答案 0 :(得分:1)
我认为罪魁祸首就在这里:
.asDriverOnErrorJustComplete()
.map { [unowned self] in
if let token = $0.token {
return self.decodeUserId(token: token)
}
return ""
}
如果登录错误或完成而没有发出next
元素,那么地图中的令牌处理将不会执行任何操作,并且任何内容都不会传递到flatMap
的链中。
至于清理你的代码......我宁愿看到这样的东西:
func fetchUser(loginAction: Driver<Void>, domain: Driver<String>, email: Driver<String>, password: Driver<String>)->Driver<User>{
let credentials = Driver.combineLatest(domain, email, password)
let latestCredentials = loginAction.withLatestFrom(credentials)
let loginResult = latestCredentials.flatMapLatest { [unowned self] domain, username, password in
self.useCase.login(params: ["domain": domain, "username": username, "password": password])
.trackActivity(self.activityIndicator)
.trackError(self.errorTracker)
.asDriverOnErrorJustComplete()
}
let token = loginResult.map { result in
guard let token = result.token else { return "" }
return token
}
let userID = token.map { [unowned self] in self.decodeUserId(token: $0) }
return userID.flatMapLatest { userID in
return self.useCase.getUser(params: ["userId": userId])
.trackActivity(self.activityIndicator)
.trackError(self.errorTracker)
.asDriverOnErrorJustComplete()
}
}