Swift CurrentValueSubject的订阅服务器如何捕获错误
我正在使用Swift CurrentValueSubject的订阅服务器如何捕获错误,swift,combine,Swift,Combine,我正在使用CurrentValueSubject填充diffabledatasource表 我怎样才能抓住错误 var strings=CurrentValueSubject([String]()) 现在receiveCompletion接收到错误,但提到使用.catch,但我看不出这种方法在这种情况下是否有效?您可以使用.catch基本上用一个有效的[String]替换错误(在这种情况下可能是一个空数组): 补充阅读: viewModel.strings .receive(on: Di
CurrentValueSubject
填充diffabledatasource表
我怎样才能抓住错误
var strings=CurrentValueSubject([String]())
现在receiveCompletion接收到错误,但提到使用
.catch
,但我看不出这种方法在这种情况下是否有效?您可以使用.catch
基本上用一个有效的[String]
替换错误(在这种情况下可能是一个空数组):
补充阅读:
viewModel.strings
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: {
print("completion \($0)")
}, receiveValue: { [weak self] in
self?.applySnapshot()
})
.store(in: &cancellables)
.receive(on: DispatchQueue.main)
.catch { error -> Just<[String]> in
print(error)
return Just([])
}
.sink(receiveValue: { [weak self] _ in
self?.applySnapshot()
})
.store(in: &cancellables)
.receive(on: DispatchQueue.main)
.replaceError(with: [])
.sink(receiveValue: { [weak self] _ in
self?.applySnapshot()
})
.store(in: &cancellables)