Subject

Subject๋Š” ์š”์†Œ๋ฅผ ๋ฐœํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์™ธ๋ถ€ ํ˜ธ์ถœ์ž์—๊ฒŒ ๋ฉ”์†Œ๋“œ๋ฅผ ๋…ธ์ถœํ•˜๋Š” Publisher๋‹ค.

Subject๋„ Publisher์ธ๋ฐ, ์ŠคํŠธ๋ฆผ์— ๊ฐ’์„ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.

Combine์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…๋ นํ˜• ์ฝ”๋“œ๋ฅผ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

send() ๋ฉ”์†Œ๋“œ ํŒจ๋ฐ€๋ฆฌ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

  • Output ํƒ€์ž…์ด Void์ผ ๋•Œ send() ๋ฉ”์†Œ๋“œ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์œผ๋‚˜ ์ „๋‹ฌํ•  ํ•„์š”๋Š” ์—†์Œ์„ ์•Œ๋ฆฐ๋‹ค.

    • send(Void())๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Void ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

  • send(_:) ๋ฉ”์†Œ๋“œ๋กœ Subscriber์—๊ฒŒ ๊ฐ’์„ ์ „๋‹ฌํ•œ๋‹ค.

  • send(completion:) ๋ฉ”์†Œ๋“œ๋กœ Subscriber์—๊ฒŒ ์—๋Ÿฌ ๋˜๋Š” ์ข…๋ฃŒ ์‹ ํ˜ธ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

  • send(subscription:) ๋ฉ”์†Œ๋“œ๋กœ Subscriber์—๊ฒŒ ๊ตฌ๋… ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

Combine์—์„œ Subject๋Š” ํ”„๋กœํ† ์ฝœ์ด๋‹ค. Publisher ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๋ฉฐ, ๊ตฌ์กฐ์ฒด๋Š” ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•  ์ˆ˜ ์—†๋‹ค.

CurrentValueSubject์™€ PassthroughSubject๊ฐ€ ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•œ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ 'Hot Observable'์˜ ๊ฐœ๋…์„ ๊ฐ–๋Š”๋‹ค.

RxSwift

RxSwift์—๋„ Subject์˜ ๊ฐœ๋…์ด ์กด์žฌํ•œ๋‹ค. ์ด๋“ค์€ SubjectType ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•œ๋‹ค.

AsyncSubject / BehaviorSubject / PublishSubject / ReplaySubject ๋“ฑ์ด ์กด์žฌํ•œ๋‹ค.

Combine์˜ CurrentValueSubject๋Š” ์ดˆ๊ธฐ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ RxSwift์˜ BehaviorSubject์™€ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค.

Combine์˜ PassthroughSubject๋Š” ์ดˆ๊ธฐ๊ฐ’์„ ๊ฐ–์ง€ ์•Š๊ณ  ์ตœ๊ทผ์— ๋ฐœํ–‰๋œ ์š”์†Œ๋ฅผ ๊ธฐ์–ตํ•˜๋Š” ๋ฒ„ํผ๋ฅผ ๊ฐ–์ง€ ์•Š์œผ๋ฏ€๋กœ RxSwift์˜ PublishSubject์™€ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค.

ReactiveSwift

ReactiveSwift์—๋„ Subject์˜ ๊ฐœ๋…์ด ์กด์žฌํ•œ๋‹ค. Property๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๊ตฌํ˜„๋˜์–ด PropertyProtocol ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•œ๋‹ค.

Property / MutableProperty ๋“ฑ์ด ์กด์žฌํ•œ๋‹ค. Property๋Š” ์ฝ๊ธฐ ์ „์šฉ์ธ ๋ฐ˜๋ฉด์— MutableProperty๋Š” ์ฝ๊ณ  ์“ฐ๊ธฐ๊ฐ€ ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜๋‹ค.

Combine๊ณผ RxSwift๊ฐ€ ์ดˆ๊ธฐ๊ฐ’์„ ๊ฐ–๋Š” Subject์™€ ์ดˆ๊ธฐ๊ฐ’์„ ๊ฐ–์ง€ ์•Š๋Š” Subject๋ฅผ ๊ตฌ๋ถ„ํ•ด ๋†“์€ ๋ฐ˜๋ฉด ReactiveSwift์˜ Property๋Š” ์ดˆ๊ธฐ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

๊ฐ Property์—์„œ Signal ๋˜๋Š” SignalProducer๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ๊ฐ 'Hot Observable'๊ณผ 'Cold Observable'์˜ ๊ฐœ๋…์„ ์ ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ์„œํ™”๋œ ํ•ญ๋ชฉ

Last updated