简体   繁体   中英

RxSwift code runs first time, second time partially

I'm learning RxSwift and i've encountered a problem with login logic.

My code runs ok? once, and partially second time, after logout.

This is from my 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>
}

It works first time, login retrieves object which contains a token string, in map part its decoded to get user id, and getUser fetches user by id.

In view controller i have this binding method:

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)
}

Both successBinding and errorBinding are UIBindingObservers, i think they are not the issue here. After logout (returning to login, pop) when i try tapping loginButton only the first part runs, login method and retrieves a token successfully, but it doesn't go to mapping logic, or user fetching logic. Any help would be appreciated.

I think the culprit is here:

.asDriverOnErrorJustComplete()
.map { [unowned self] in
    if let token = $0.token {
        return self.decodeUserId(token: token)
    }
    return ""
}

If the login errors or completes without emitting a next element, then the token processing in the map will not do anything and nothing will get passed down the chain in that flatMap .

As for cleaning up your code... I would rather see something like this:

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM