我正在尝试使用两个单独的函数调用来验证用户的电子邮件和密码。
这两个函数都返回AnyPublisher发布者,并且我使用CombineLatest将返回的值(每个validate调用返回其正在验证的字符串)收集到一个元组中。
然后,我使用flatMap发出网络请求,以使用CombineLatest返回的值来注册用户,但是flatMap运算符从未被调用。
validator.validate(text: email, with: [.validEmail])
.combineLatest(validator.validate(text: password, with: [.notEmpty]))
.flatMap { credentials in
return self.userSessionRepository.signUp(email: credentials.0, password: credentials.1)
}
.sink(receiveCompletion: { completion in
switch completion {
case .failure(let error):
print(error)
self.indicateErrorSigningIn(error)
case .finished:
self.goToSignInNavigator.navigateToOtp()
}
}, receiveValue: { _ in })
.store(in: &subscriptions)
signUp(email:password :)返回AnyPublisher
这是验证函数:
public func validate(text: String, with rules: [Rule]) -> AnyPublisher<String, ErrorMessage> {
rules.publisher
.compactMap { $0.check(text) }
.setFailureType(to: ErrorMessage.self)
.flatMap {
Fail<Void, ErrorMessage>(error: ErrorMessage(title: "Error", message: $0.description))
}
.map { text }
.eraseToAnyPublisher()
}
和signUp函数:
public func signUp(email: String, password: String) -> AnyPublisher<Void, ErrorMessage> {
remoteAPI.signUp(email: email, password: password)
.flatMap(dataStore.save)
.mapError { error -> ErrorMessage in
return ErrorMessage(title: "Error", message: error.description)
}
.eraseToAnyPublisher()
}
它调用了以下两个函数:
public func signUp(email: String, password: String) -> AnyPublisher<Confirmation, RemoteError> {
guard email == "[email protected]" else {
return Fail<Confirmation, RemoteError>(error: .invalidCredentials)
.eraseToAnyPublisher()
}
return Just(Confirmation(otp: "", nonce: "abcd"))
.setFailureType(to: RemoteError.self)
.eraseToAnyPublisher()
}
public func save(confirmation: Confirmation) -> AnyPublisher<Void, RemoteError> {
self.nonce = confirmation.nonce
return Empty().eraseToAnyPublisher()
}
我不确定有什么问题,尽管由于我最近才开始学习它,可能是我对“组合”不够了解。
我已经知道了。
问题出在validate(text:with :)函数上。
在发生错误的情况下,该函数的行为正确,但是在没有错误的情况下,该函数不会发出任何值,这就是为什么未调用flatMap或管道中的任何其他运算符的原因。
它没有发出任何值的原因归结为在compactMap中调用的check(_ :)函数的工作方式。它返回一个可选的字符串,这是一条错误消息。但是,如果没有错误,就没有字符串,因此不会发出任何值。
结果,不会评估对.map {text}的调用,也不会返回凭据。
我将代码更改为此,现在程序可以正常运行:
public func validate(text: String, with rules: [Rule]) -> AnyPublisher<String, ErrorMessage> {
rules.publisher
.setFailureType(to: ErrorMessage.self)
.tryMap { rule -> String in
if let error = rule.check(text) {
throw ErrorMessage(title: "Error", message: error)
}
return text
}
.mapError { error -> ErrorMessage in
return error as! ErrorMessage
}
.eraseToAnyPublisher()
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句