【发布时间】:2023-04-10 06:21:01
【问题描述】:
我正在考虑 Async Await 功能,但我陷入了涉及提要的后备方案。在我的重构中,viewModel 尝试从网络获取提要,或者如果它失败了默认回退。
我脑子里的问题是 async await 应该消除嵌套闭包问题,但我仍然认为在后备用例中需要它。
func fetch() async throws -> [Feed]? {
var feed: [Feed]?
do {
feed = try await remoteLoader.load()
} catch {
// Pass error up the chain for handling
do {
feed = try await localLoader.load()
} catch {
// Pass error up the chain for handling
}
}
return feed
}
这是回退的预期方法吗?
我想出的另一种方法是先尝试加载本地,然后如果成功则将其替换为远程。
func fetch() async throws -> [Feed]? {
var feed: [Feed]?
do {
feed = try await localLoader.load()
feed = try await remoteLoader.load()
} catch {
throw FeedRepositoryError.FetchFeed(error)
}
return feed
}
【问题讨论】:
标签: swift async-await