gpt4 book ai didi

Swift Combine - 如何抛出错误并停止执行

转载 作者:行者123 更新时间:2023-12-04 09:03:30 29 4
gpt4 key购买 nike

我有一个 HTTP 请求发布者,当返回 401 错误时,我想停止执行并显示我的登录屏幕。
这是我的代码的一部分:

cancellable = fetcher.hello(helloRequest: HelloRequest(name: self.name))
.print("fetcher.hello")
.catch { _ in
// TODO: how to handle errors with request?
Just(HelloResponse.placeHolder)
}
.flatMap { response -> AnyPublisher<HelloResponse, Never> in
if response.imageUrl == nil || response.imageUrl == "" {
// If there's no image to download just return the response
return Just(response).eraseToAnyPublisher()
}
else {
// Chain together request and download image
return fetcher.downloadImage(url: response.imageUrl!)
.print("fetcher.hello.downloadImage")
.catch { _ in
// If there was an error downloading the image, replace it with a placeholder
Just(UIImage(named: "placeholder_square")!)
}
.map {
// Add image to response
HelloResponse(message: response.message, visitCount: response.visitCount, imageUrl: response.imageUrl, image: $0)
}
.eraseToAnyPublisher()
}
}
.sink(receiveCompletion: { _ in }, receiveValue: { self.response = $0.self })
所以问题的一部分是下面的 flatMap 会在必要时下载图像。输出类型是 AnyPublisher (我想不出另一种方法)。现在,catch 返回一个占位符模型并且工作正常。但现在我已经接受了这个错误。我认为 Empty() 发布者可能会工作,但它似乎不正确。我试过 Fail() 但显然 catch 是一个 Never (有道理)。谢谢!

最佳答案

使用 New Dev 关于 setFailureType 的建议,我现在可以正常工作了。当我最初让 flatMap 返回 AnyPublisher 时,它不会编译,因为 Just 是 Never。所以我将 flatMap 更改为 Never fail 但后来我无法删除 catch,因为之前的失败类型是 Error。将 setFailureType 添加到 flatMap 中的 Justs 现在让我更改 flatMap 以返回错误,并且我删除了捕获。

cancellable = fetcher.hello(helloRequest: HelloRequest(name: self.name))
.print("fetcher.hello")
.flatMap { response -> AnyPublisher<HelloResponse, Error> in
if response.imageUrl == nil || response.imageUrl == "" {
// If there's no image to download just return the response
return Just(response)
.setFailureType(to: Error.self) // This allows us to set a failure type (Just is Never) so that it will match AnyPublisher<HelloResponse, Error>
.eraseToAnyPublisher()
}
else {
// Chain together request and download image
return fetcher.downloadImage(url: response.imageUrl!)
.print("fetcher.hello.downloadImage")
.catch { _ in
// If there was an error downloading the image, replace it with a placeholder
Just(UIImage(named: "placeholder_square")!)
.setFailureType(to: Error.self) // This allows us to set a failure type (Just is Never) so that it will match AnyPublisher<HelloResponse, Error>
}
.map {
// Add image to response
HelloResponse(message: response.message, visitCount: response.visitCount, imageUrl: response.imageUrl, image: $0)
}
.eraseToAnyPublisher()
}
}
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
break
case .failure(let error):
print("Request error: \(String(describing: error))")
}
}, receiveValue: {
self.response = $0.self
})
}

关于Swift Combine - 如何抛出错误并停止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63511459/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com