我正在尝试使用两个单独的函数调用来验证用户的电子邮件和密码。
两个函数都返回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()
}
我不确定什么地方出了问题,尽管我最近才刚开始学习它,但可能我还不太了解Combine。
我知道了。
问题出在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()
}