Combine Code Review
Quick Reference
| Issue Type | Reference |
|---|
| Publishers, Subjects, AnyPublisher | references/publishers.md |
| map, flatMap, combineLatest, switchToLatest |
references/operators.md |
| AnyCancellable, retain cycles, [weak self] |
references/memory.md |
| tryMap, catch, replaceError, Never |
references/error-handling.md |
Review Checklist
- - [ ] All
sink closures use [weak self] when self owns cancellable - [ ] No
assign(to:on:self) usage (use assign(to: &$property) or sink) - [ ] All AnyCancellables stored in Set or property (not discarded)
- [ ] Subjects exposed as
AnyPublisher via INLINECODE5 - [ ]
flatMap used correctly (not when map + switchToLatest needed) - [ ] Error handling inside
flatMap to keep main chain alive - [ ]
tryMap followed by mapError to restore error types - [ ]
receive(on: DispatchQueue.main) before UI updates - [ ] PassthroughSubject for events, CurrentValueSubject for state
- [ ] Future wrapped in Deferred when used with retry
When to Load References
- - Reviewing Subjects or publisher selection → publishers.md
- Reviewing operator chains or combining publishers → operators.md
- Reviewing subscriptions or memory issues → memory.md
- Reviewing error handling or try* operators → error-handling.md
Review Questions
- 1. Are all subscriptions being retained? (Check for discarded AnyCancellables)
- Could any sink or assign create a retain cycle with self?
- Does flatMap need to be switchToLatest for search/autocomplete?
- What happens when this publisher fails? (Will it kill the main chain?)
- Are error types preserved or properly mapped after try* operators?
Combine 代码审查
快速参考
references/operators.md |
| AnyCancellable、循环引用、[weak self] |
references/memory.md |
| tryMap、catch、replaceError、Never |
references/error-handling.md |
审查清单
- - [ ] 当 self 持有 cancellable 时,所有 sink 闭包都使用 [weak self]
- [ ] 没有使用 assign(to:on:self)(应使用 assign(to: &$property) 或 sink)
- [ ] 所有 AnyCancellable 存储在 Set 或属性中(未被丢弃)
- [ ] 通过 eraseToAnyPublisher() 将 Subjects 暴露为 AnyPublisher
- [ ] 正确使用 flatMap(非需要 map + switchToLatest 的情况)
- [ ] 在 flatMap 内部处理错误以保持主链存活
- [ ] tryMap 后跟 mapError 以恢复错误类型
- [ ] UI 更新前使用 receive(on: DispatchQueue.main)
- [ ] 事件使用 PassthroughSubject,状态使用 CurrentValueSubject
- [ ] 与 retry 一起使用时,Future 包裹在 Deferred 中
何时加载参考
- - 审查 Subjects 或发布者选择 → publishers.md
- 审查操作符链或组合发布者 → operators.md
- 审查订阅或内存问题 → memory.md
- 审查错误处理或 try* 操作符 → error-handling.md
审查问题
- 1. 所有订阅是否都被保留?(检查是否有被丢弃的 AnyCancellable)
- 是否有任何 sink 或 assign 与 self 形成循环引用?
- 搜索/自动补全场景中,flatMap 是否需要替换为 switchToLatest?
- 当此发布者失败时会发生什么?(是否会终止主链?)
- 在 try* 操作符之后,错误类型是否被保留或正确映射?