输入新电子邮件时如何重置和启动新计时器

How to reset and start new timer when enter new email

我有一个接受字符串的输入,并将其存储在本地数据中。我的用例是当我输入电子邮件时,用户必须等待 2 分钟才能请求发送电子邮件验证。但是当我输入不同的电子邮件名称时,当我输入的最后一封电子邮件仍在倒计时时,我无法重置计时器。我正在使用 RxSwift 作为计时器,我不知道如何在 RxSwift 中使计时器无效。

这就是我到目前为止在用户输入新电子邮件时重置计时器的方法

// function that accept email from uitextfield.text
func resendEmailCountdown(with email: String) {
        if email != getLoggedEmail() {
            startCountdown(countdown: 0)
            startCountdown(countdown: 120)
        } else {
            startCountdown(countdown: 120)
        }
    }
    
    private func startCountdown(countdown: Int) {
        let counter = countdown
        if counter == 0 {
            _ = Observable<Int>.timer(.seconds(0), period: .seconds(1), scheduler: MainScheduler.instance)
                .take(0)
                .subscribe(onNext: { [weak self] countdown in
                    guard let self = self else { return }
                    
                }).disposed(by: disposeBag)
        } else {
            _ = Observable<Int>.timer(.seconds(0), period: .seconds(1), scheduler: MainScheduler.instance)
                .take(counter + 1)
                .subscribe(onNext: { [weak self] countdown in
                    guard let self = self else { return }
                    let count = counter - countdown
                    if count != 0 {
                        self.eventResendEmailCountdown.onNext(count)
                        self.eventShowHideResendEmailButton.onNext(false)
                    } else {
                        self.eventShowHideResendEmailButton.onNext(true)
                        self.eventDismissCountdownBottomSheet.onNext(())
                    }
                }).disposed(by: disposeBag)
        }
        
    }

钥匙正在使用flatMapLatest取消之前的计时器并启动一个新的。

根据您的描述,这是您需要的:

struct Output {
    let eventResendEmailCountdown: Observable<Int>
    let eventShowHideResendEmailButton: Observable<Bool>
    let eventDismissCountdownBottomSheet: Observable<Void>
}

func example(text: Observable<String?>) -> Output {
    let trigger = text.share()
    let eventResendEmailCountdown = trigger
        .flatMapLatest { _ in
            Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
                .map { 120 - [=10=] }
                .take(until: { [=10=] == -1 })
        }
    let eventShowHideResendEmailButton = Observable.merge(
        trigger.map { _ in false },
        eventResendEmailCountdown.filter { [=10=] == 0 }.map { _ in true }
    )
    let eventDismissCountdownBottomSheet = eventResendEmailCountdown
        .filter { [=10=] == 0 }
        .map { _ in }

    return Output(
        eventResendEmailCountdown: eventResendEmailCountdown,
        eventShowHideResendEmailButton: eventShowHideResendEmailButton,
        eventDismissCountdownBottomSheet: eventDismissCountdownBottomSheet
    )
}

可以这样称呼它:

let output = example(text: textField.rx.text.asObservable())

output.eventResendEmailCountdown
    .debug("eventResendEmailCountdown")
    .subscribe()

output.eventShowHideResendEmailButton
    .debug("eventShowHideResendEmailButton")
    .subscribe()

output.eventDismissCountdownBottomSheet
    .debug("eventDismissCountdownBottomSheet")
    .subscribe()