ios - 忽略组合中的错误
问题描述
在 RxSwift 中,我可以忽略错误,例如
.catchError { _ in Observable.never() }
为什么这在组合中不起作用(例如,它仍然完成)
.catch { _ in Empty(completeImmediately: false) }
struct SimpleError: Error {}
let numbers = [5, 4, 3, 2, 1, 0, 9, 8, 7, 6]
let cancellable = numbers.publisher
.tryFilter { int -> Bool in
guard int != 0 else {throw SimpleError()}
return true
}
.catch { _ in Empty(completeImmediately: false) }
.sink {
print("\($0)")
}
完成投掷。输出是:
5
4
3
2
1
解决方案
我很高兴有人能够在评论中帮助你,因为你在问为什么你的代码显然没有完成。
struct SimpleError: Error {}
let numbers = [5, 4, 3, 2, 1, 0, 9, 8, 7, 6]
let cancellable = numbers.publisher
.tryFilter { int -> Bool in
guard int != 0 else {throw SimpleError()}
return true
}
.catch { _ in Empty(completeImmediately: false) }
.print("")
.sink { _ in }
以上显然不会发出“完成”事件。如果您更改completeImmediately
为true
,那么您将看到它确实如此。
推荐阅读
- javascript - 混淆 Electron 应用程序的 background.js(在 Next.js 框架中,nextron)
- javascript - 如何在 html5 画布上使用箭头键平滑地制作形状
- c++ - 标准库 `emplace` 函数是否使用 `std::in_place` 标记
- flutter - 颤振视频播放器问题
- java - 如何在 Android 应用程序中使用地图框创建自定义 Air 地图视图并绘制多边形
- python - Pandas Dataframe 无法存储在 SQLite3 数据库中
- jquery - 如何在 WordPress 管理员上运行 jQuery 代码?
- linux - 为什么 x86_64 的 vdso 中没有实现 getpid()?
- java - 将排序的双向链表转换为平衡的二叉搜索树?这个递归是如何工作的?
- python - 如何在 tkinter 的另一个画布内制作画布的透明背景?