refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게 [공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류. 문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인). - Topic_Programming → Domain_Programming (내부 구조 보존) - Topic_Graphic → Domain_Design - Topic_Business → Domain_Product - Topic_General → Domain_General - _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning), Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing) - 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서) - 빈 폴더 정리 (memory/procedures) - 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
---
|
||||
id: ios-combine-patterns
|
||||
title: Combine — 비동기 stream / 변환 / cancel
|
||||
category: Coding
|
||||
status: draft
|
||||
source_trust_level: B
|
||||
verification_status: conceptual
|
||||
created_at: 2026-05-09
|
||||
updated_at: 2026-05-09
|
||||
tags: [ios, combine, swift, async, vibe-coding]
|
||||
tech_stack: { language: "Swift / Combine", applicable_to: ["iOS 13+"] }
|
||||
applied_in: []
|
||||
aliases: [Publisher, Subscriber, AnyCancellable, sink]
|
||||
---
|
||||
|
||||
# Combine — 비동기 stream
|
||||
|
||||
> Apple 의 reactive framework. async/await 가 더 단순하지만, Combine 은 **변환 체인 / 동시 source 결합 / 자동 cancel** 에서 강함. SwiftUI 와 자연스럽게 통합.
|
||||
|
||||
## 📖 핵심 개념
|
||||
- Publisher: 값 stream emit.
|
||||
- Subscriber: 값 받기.
|
||||
- Operator: map / filter / debounce / combineLatest / merge.
|
||||
- AnyCancellable: subscription handle. dispose 시 자동 cancel.
|
||||
|
||||
## 💻 코드 패턴
|
||||
|
||||
### Search debounce (typical)
|
||||
```swift
|
||||
import Combine
|
||||
|
||||
final class SearchViewModel: ObservableObject {
|
||||
@Published var query: String = ""
|
||||
@Published var results: [Item] = []
|
||||
private var bag = Set<AnyCancellable>()
|
||||
|
||||
init() {
|
||||
$query
|
||||
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
|
||||
.removeDuplicates()
|
||||
.filter { $0.count >= 2 }
|
||||
.map { q in
|
||||
URLSession.shared.dataTaskPublisher(for: searchURL(q))
|
||||
.map(\.data)
|
||||
.decode(type: [Item].self, decoder: JSONDecoder())
|
||||
.replaceError(with: [])
|
||||
}
|
||||
.switchToLatest() // 이전 검색 cancel
|
||||
.receive(on: DispatchQueue.main)
|
||||
.assign(to: &$results)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### combineLatest
|
||||
```swift
|
||||
Publishers.CombineLatest3($email, $password, $agreedToTerms)
|
||||
.map { e, p, a in e.contains("@") && p.count >= 8 && a }
|
||||
.assign(to: &$canSubmit)
|
||||
```
|
||||
|
||||
### Cancel
|
||||
```swift
|
||||
let task = publisher.sink(receiveValue: { ... })
|
||||
// 화면 disappear
|
||||
task.cancel()
|
||||
// 또는 bag.removeAll()
|
||||
```
|
||||
|
||||
### async/await 와 통합
|
||||
```swift
|
||||
let value = try await publisher.values.first { _ in true }!
|
||||
|
||||
// Or convert async to publisher
|
||||
extension Publisher {
|
||||
func toAsync() async throws -> Output { ... }
|
||||
}
|
||||
```
|
||||
|
||||
### NotificationCenter — Combine
|
||||
```swift
|
||||
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
|
||||
.sink { _ in self.refresh() }
|
||||
.store(in: &bag)
|
||||
```
|
||||
|
||||
## 🤔 의사결정 기준
|
||||
| 상황 | Combine | async/await |
|
||||
|---|---|---|
|
||||
| 한 번 호출 | ❌ | ✅ |
|
||||
| Stream of values (search, websocket) | ✅ | AsyncSequence |
|
||||
| 여러 source 결합 | ✅ — combineLatest | 직접 await |
|
||||
| SwiftUI @Published / assign | ✅ | manual setState |
|
||||
| iOS 13 호환성 | ✅ | iOS 15+ |
|
||||
| Time-based (debounce, throttle) | ✅ | 직접 구현 |
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **AnyCancellable 무시**: `_ = publisher.sink(...)` — 즉시 deallocate. store(in: &bag).
|
||||
- **메인 스레드에서 무거운 변환**: receive(on: DispatchQueue.global()) 으로 분기.
|
||||
- **switchToLatest 안 씀**: 검색 결과 race. 이전 결과가 새 결과 덮어씀.
|
||||
- **assign 후 weak self 안 함**: cycle. assign(to: &$x) 또는 sink + [weak self].
|
||||
- **error 처리 누락**: .sink 의 receiveCompletion 무시 → silent failure.
|
||||
- **모든 곳 Combine**: async/await 가 더 단순한 경우 많음.
|
||||
|
||||
## 🤖 LLM 활용 힌트
|
||||
- 검색 / 스트림 / SwiftUI binding = Combine 적합.
|
||||
- 한 번 호출 = async/await.
|
||||
|
||||
## 🔗 관련 문서
|
||||
- [[iOS_Swift_Concurrency_async_await]]
|
||||
- [[iOS_SwiftUI_State_Property_Wrappers]]
|
||||
Reference in New Issue
Block a user