[Combine] Subscribe

๊ตฌ๋…ํ•˜๊ธฐ๐Ÿ•ต๐Ÿปโ€โ™€๏ธ

naljin
11 min readFeb 23, 2020

์ด์ „ ํฌ์ŠคํŒ…์—์„œ๋Š” Publisher์™€ Subscriber์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค! ์•ˆ์ฝ๊ณ  ์˜ค์…จ๋‹ค๋ฉด ๊ฐ๊ฐ์„ ํด๋ฆญํ•ด์„œ ์ฝ๊ณ  ์˜ค์‹œ๋Š”๊ฒŒ..๋„์›€์ด..๋ ๊ฑธ์š”..?๐Ÿค”

์ž์ž..! subscriber๋Š” ์–ด๋–ป๊ฒŒ publisher๋ฅผ ๊ตฌ๋…ํ•˜๊ณ  ๊ฐ’์„ ๋ฐ›๋Š”๊ฑธ๊นŒ์š”?

๋ฐ”๋กœprotocol Publisher ์— ์ •์˜๋˜์–ด ์žˆ๋Š” subscribe(_:) ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ง์ด์ฃ .

์จ˜~~ ์ด์ œ subscribe(_:) ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋“ค์–ด๊ฐ„ subscriber๋Š” publisher๊ฐ€ ์ƒ์‚ฐํ•˜๋Š” ๊ฐ’๋“ค์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค~~ ์–ด๋•Œ์š” ์ฐธ ์‰ฝ์ฃ ?

์‚ฌ์‹ค ์ „ ์ด๊ฑฐ ๋ณธ์  ์—…์๋‹ˆ๋‹ค..์•Œ์ง€๋ชจ๋‹ต๋‹ˆ๋‹คโ€ฆ.

ํ•˜์ง€๋งŒ ์ข€ ๋” ๋“ค์–ด๊ฐ€๋ด…์‹œ๋‹ค. ์ € ์•ˆ์—์„œ๋Š” ์–ด๋–ค์ผ๋“ค์ด ๋ฒŒ์–ด์ง€๊ณ  ์žˆ๋Š”๊ฑธ๊นŒ์š”?๐Ÿค” ์ฐจ๊ทผ์ฐจ๊ทผ ์‚ดํŽด๋ณด์•„์š”๐Ÿ‘ฃ

๋ฐฐ๊ฒฝ

์ดํ•ด๋ฅผ ๋•๊ธฐ์œ„ํ•ด ํ˜„์ƒ์„ ๋Œ์–ด์™€๋ด…์‹œ๋‹ค. ์œ ํŠœ๋ธŒ๋ฅผ ์˜ˆ๋กœ ๋“ค์–ด๋ณผ๊ฒŒ์š”.

์š”์ฆ˜ ๊ฐ€์ˆ˜ โ€˜๋Ÿฌ๋ธŒ์†กโ€™๋‹˜์—๊ฒŒ ์ž…๋ฌธํ•ด๋ฒ„๋ ธ๋‹คโ€ฆ

๋ณด๋‹ค๋ณด๋‹ˆ ์šฐ๋Ÿฐ๋‹ˆ..๋…ธ๋ž˜ ๋„˜ ์ž˜๋ถ€๋ฅด๊ณ โ€ฆใ…œใ…œใ…œ

์•—,, ์œ ํˆฌ๋ธŒ๋„ ํ•˜์ž–์•„?! ๋ฐ”๋กœ ๊ตฌ๋…ํ•ด์•ผ์ง€..

์•ž์œผ๋ก  ์šฐ๋Ÿฐ๋‹ˆ์˜ ๋ชจ๋“ ๊ฒƒ..๋†“์น˜์ง€ ์•Š์„๊ฑฐ์‹œ๋‹ค..

์ด๋Ÿฐ ์ƒํ™ฉ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์„๊ฑฐ์˜ˆ์š”.

๋‚˜ = โ€˜๋Ÿฌ๋ธŒ์†กโ€™๋‹˜์˜ ๊ตฌ๋…์„ ์›ํ•˜๋Š” ๊ตฌ๋…์ž (subscriber)

๋Ÿฌ๋ธŒ์†ก = ์ปจํ…์ธ  ์ƒ์‚ฐ์ž (publisher)

๋‚ด๊ฐ€ ์ง€๊ธˆ ์›ํ•˜๋Š” ๊ฒƒ = โ€˜๋Ÿฌ๋ธŒ์†กโ€™๋‹˜ ๊ตฌ๋… (subscribe)

**๋ง‰๊ฐ„ ์šฐ๋Ÿฐ๋‹ˆ ํ™๋ณด**

์šฐ๋Ÿฐ๋‹ˆ.. ๋Ÿฌ๋ธŒ์†ก ์น˜๋ฉด.. ์น ๊ณต์ฃผ ๋Ÿฌ๋ธŒ์†ก๋งŒ ๋‚˜์™€์„œ ๋„˜ ์†์ƒํ•ดโ€ฆ ํฐ๋ˆˆ์ด ๊ธฐ์จ๋˜๋Š”๋‚ .. ํฐ๋ˆˆ์ด ๋ฏธ์†Œ๋˜๋Š”๋‚ โ€ฆใ…  โ€๏ธ

๋งŒ์•ฝ ์„ธ์ƒ ํž™ํ•œ์ฒ™ ํ•˜๊ณ  ์‹ถ๋‹คโ€ฆ ๋‚˜๋งŒ ์•„๋Š” ์ข‹์€ ๋…ธ๋ž˜ ์žˆ๊ณ  ์‹ญ๋”ฐ.. ํ™๋Œ€๋ณ‘ ๋ง๊ธฐ๋‹ค..ํ•˜๋Š” ๋ถ„๋“ค.. ๊ตฌ๋…..๐Ÿ‘..๐Ÿ””์„ค์ •๊นŒ์ง€โ€ฆ! ๐Ÿˆ์—์„œ ์Šค๋ฐ ๋ฐฑ๋ฒˆ ๋Œ๋ ค..!!!!!!!!!

๊ณผ์ •

  1. ๋จผ์ € โ€˜๋Ÿฌ๋ธŒ์†กโ€™๋‹˜(publisher)์—๊ฒŒ ๊ตฌ๋…์„ ์š”์ฒญํ•ด์•ผ๊ฒ ์ฃ ? ์ด๋ฅผ ์œ„ํ•ด์„œ ์•ž์„œ ์‚ดํŽด๋ณด์•˜๋“ฏ, publisher์— ๋”ธ๋ฆฐ subscribe(_:) ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด subscriber์˜ ๊ตฌ๋…์„ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

2. subscribe(_:) ์˜ ๊ตฌํ˜„๋ถ€์—์„œ๋Š” receive(subscriber:)๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” Publisher ๋ผ๋ฉด ๋ชจ๋‘ ๊ตฌํ˜„(implement)ํ•ด์•ผํ•˜๋Š” ํ•จ์ˆ˜๋กœ์„œ protocol Publisher์— ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

3. receive(subscriber:) ์—์„œ๋Š” ๋ฌด์—‡์„ ํ• ๊นŒ์š”? ๋ฐ”๋กœ publisher์™€ subscriber์„ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค. ์–ด๋–ป๊ฒŒ์š”? ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ ์„œ ์„ค๋ช…ํ•ด ๋ณผ๊ฒŒ์š”.

  • ๊ตฌ๋…์„ ์š”์ฒญํ–ˆ์œผ๋ฉด ๊ตฌ๋…๊ถŒ์ด ํ•„์š”ํ•˜๊ฒ ์ฃ ? ์šฐ์„  subscription์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง„ subscription์„ subscriber์—๊ฒŒ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด subscriber์˜ receive(subscription:)์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋Š” protocol Subscriber์— ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ publisher์™€ subscriber๋Š” subscription ์„ ํ†ตํ•ด ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค.

4.receive(subscription:) ์—์„œ๋Š” protocol Subscription ์— ์ •์˜๋œ request(_:)์„ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ’์„ ์ตœ๋Œ€๋กœ ์–ผ๋งŒํผ ๋ฐ›์„ ๊ฒƒ์ธ์ง€ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜์˜ parameter type์ด Demand๋ผ๋Š”๊ฑธ ๊ธฐ์–ตํ•ด๋‘์„ธ์šฉ. 5๋ฒˆ์— ๋‹ค์‹œ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค!

3๊ฐœ ์ด์ƒ์œผ๋กœ ๊ฐ’ ๋“ค์–ด์˜ฌ ์ˆ˜ ์—†๋‹ค!

protocol Subscription ์— ์ •์˜๋œ ๊ฑธ ๋ณด๋‹ˆ subscriber๊ฐ€ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์€ subscription ์˜ ์—ญํ• ์ธ๊ฐ€๋ด‰๊ฐ€? ๋ผ๊ณ  ์ƒ๊ฐํ• ์ˆ˜ ์žˆ๊ฒ ์ฃ ? subscription ์˜ ์—ญํ• ์„ ์ข€ ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณผ๊นŒ์š”?

  • publisher์—์„œ ์ƒˆ๋กœ์šด ๊ฐ’์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ publisher ์™€ subscriber ์„ ์ค‘์žฌํ•˜๊ณ  subscriber๊ฐ€ ์š”์ฒญํ•œ ๊ฒƒ๋ณด๋‹ค ๋” ๋งŽ์€ ๊ฐ’์„ ๋ฐ›์ง€ ์•Š๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค
  • subscribers ์˜ ์œ ์ง€ ๋ฐ ํ•ด์ œ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค

5.subscriber์˜ receive(_:)๋ฅผ ํ†ตํ•ด publisher๊ฐ€ ๋ฐฉ์ถœํ•˜๋Š” ์ƒˆ๋กœ์šด ๊ฐ’๋“ค์„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐฉ๊ธˆ 4๋ฒˆ์—์„œsubscription.request(_:) ๋ฅผ ํ†ตํ•ด subscriber๊ฐ€ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ–ˆ์—ˆ์ฃ ? ์ด๋•Œ parameter ํƒ€์ž…์ด Demand ์ธ๊ฑธ ๊ธฐ์–ตํ•˜๋ผ๊ณ  ํ–ˆ์–ด์š”! ์™œ๋ƒ๋ฉด receive(_:) ์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…๋„ Demand ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค! ์ด๋ฅผ ํ†ตํ•ด์„œ ์ฒ˜์Œ์— subscriber๊ฐ€ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•˜๋”๋ผ๋„ ์ƒˆ ๊ฐ’์„ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ์ตœ๋Œ€ ๊ฐ’์„ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ์–ด์š”! ์ƒˆ๋กญ๊ฒŒ ์„ค์ •ํ•œ max ๊ฐ’์ด ๊ธฐ์กด์˜ max ๊ฐ’์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ƒˆ ๊ฐ’์„ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ๊ธฐ์กด์˜ max ๊ฐ’์„ ๋Š˜๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ ์ค„์ผ ์ˆ˜๋Š” ์—†๊ฒ ์ฃ ?

6. ๋งŒ์•ฝ publisher๊ฐ€ ๋” ์ด์ƒ ๊ฐ’์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด subscriber์˜ receive(completion:)์„ ํ˜ธ์ถœ์„ ํ†ตํ•ด ์ข…๋ฃŒ๋ฅผ ์•Œ๋ฆฝ๋‹ˆ๋‹ค.

ํ๋ฆ„๋„

์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•œ ๋‚ด์šฉ์„ ํ๋ฆ„๋„๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์•„์š”~

์กฐ๊ธˆ ๋” ์ •ํ™•ํžˆ๋Š” publisher์™€ subscriber ์‚ฌ์ด์— subscription์ด ์ƒ์„ฑ๋˜๊ณ  ๋‚˜๋ฉด ์ด๊ฒƒ์„ ๋งค๊ฐœ์ฒด๋กœ ํ•ด์„œ ์ดํ›„์˜ ๋™์ž‘๋“ค์ด ์ผ์–ด๋‚œ๋‹ค๊ณ  ๋ณด๋ฉด ๋ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค. (์•„๋งˆ? ใ…Ž๐Ÿค”)

์˜ˆ์ œ ์ฝ”๋“œ

์ข‹์•„์š”! ์ด์ œ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ์ผ๋งŒ ๋‚จ์•˜๊ตฐ์—ฌ.. ์ง์ ‘ ๊ตฌํ˜„์˜ ์ข‹์€์ ์€ ๋ชจ๋“  ๋ผ์ดํ”„์‚ฌ์ดํด์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด์ ธ~~~~

์ถœ๋ ฅ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฉ๋‹ˆ๋‹ค.

โ€”โ€”โ€” Example of: Custom Subscriber โ€”โ€”โ€”
Received value 1
Received value 2
Received value 3

๋˜์ž‰? publisher ๋Š” 1๋ถ€ํ„ฐ 6๊นŒ์ง€ ๊ฐ’์„ ๋ฐฉ์ถœํ• ํ…๋ฐ ์™œ ์„ธ ๊ฐœ ๋ฐ–์— ์•ˆ์ฐํžŒ๋‹ด?? ๋ฐ”๋กœ ์ดˆ๊ธฐ ์š”์ฒญ์—์„œ max ๊ฐ’์„ ์ง€์ •ํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ !

subscription.request(.max(3))

์ฒ˜์Œ์— ์„ค์ •ํ•œ max ๊ฐ’์„ ์ค‘๊ฐ„์— ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ๋‹ค?? ํ•˜๋ฉด receive(_:) ํ•จ์ˆ˜ ์•ˆ์„ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„์—์„œ receive(_:)๋ฅผ ์„ค๋ช…ํ• ๋•Œ ์ด๋Ÿฐ ๋‚ด์šฉ์ด ์žˆ์—ˆ๋‹ตโ€๋‹ˆ๋‹ค๐Ÿ‘†๐Ÿป

์ฒ˜์Œ์— subscriber๊ฐ€ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ์ •ํ•˜๋”๋ผ๋„ ์ƒˆ ๊ฐ’์„ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ์ตœ๋Œ€ ๊ฐ’์„ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ์–ด์š”! ์ƒˆ๋กญ๊ฒŒ ์„ค์ •ํ•œ max ๊ฐ’์ด ๊ธฐ์กด์˜ max ๊ฐ’์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ƒˆ ๊ฐ’์„ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค ๊ธฐ์กด์˜ max ๊ฐ’์„ ๋Š˜๋ฆด ์ˆ˜ ์žˆ์ง€๋งŒ ์ค„์ผ ์ˆ˜๋Š” ์—†๊ฒ ์ฃ ?

๋”ฐ๋ผ์„œ receive(_:) ์˜ return ๊ฐ’์„ .none ์—์„œ .unlimited๋กœ ๋ณ€๊ฒฝํ•ด๋ณผ๊ฒŒ์š”. ์ด๋ฆ„์—์„œ๋ถ€ํ„ฐ ์•Œ์ˆ˜ ์žˆ๋‹ค์‹œํ”ผ publisher๊ฐ€ ์ƒ์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€๋กœ ๋ฐ›์•„๋ฒ„๋ฆฌ๊ฒ ๋‹ค..๋Š” ์˜๋ฏธโ€ฆ! (A request for as many values as the publisher can produce.)

func receive(_ input: Int) -> Subscribers.Demand {
print("Received value", input)
return .unlimited
}

์จ˜๐Ÿงš๐Ÿปโ€โ™€๏ธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ๋ณ€ํ–ˆ์Šต๋‹ˆ๋‹ค

โ€”โ€”โ€” Example of: Custom Subscriber โ€”โ€”โ€”
Received value 1
Received value 2
Received value 3
Received value 4
Received value 5
Received value 6
Received completion finished

๋งŒ์•ฝ .unlimited๋ฅผ .max(1)์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ๋‹ค์‹œ ์‹คํ–‰ํ•œ๋‹ค๋ฉด? ์ƒˆ๋กญ๊ฒŒ ์„ค์ •ํ•œ max ๊ฐ’์ด ๊ธฐ์กด์˜ max ๊ฐ’์— ์ถ”๊ฐ€๋œ๋‹ค๊ณ  ํ–ˆ์œผ๋‹ˆ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค max ๊ฐ’์€ 1์”ฉ ๊ณ„์† ๋Š˜์–ด๋‚˜๊ฒ ์ฃ ?? ๋•Œ๋ฌธ์— 6๊ฐœ ๊ฐ’์ด ๋ชจ๋‘ ์ฐํž๊ฑฐ์˜ˆ์š”!

๋งˆ๋ฌด๋ฆฌ

์ด๋ ‡๊ฒŒ Subscriber๋ฅผ ์ƒ์†๋ฐ›์•„ ์ง์ ‘ ๊ตฌํ˜„ํ•œ custom subscriber ์„ ์ด์šฉํ•ด publisher๋ฅผ ๊ตฌ๋…ํ•ด๋ดค์–ด์š”! ํ•˜์ง€๋งŒ ์ด์ „ ํฌ์ŠคํŒ…์—์„œ๋„ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ์• ํ”Œ์€ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ต๋‹ˆ๋‹ค~ ๋Œ€์‹  Combine์—์„œ๋Š” Sink, Assign ๋“ฑ์˜ ๊ธฐ๋ณธ Subscriber๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๊ณ  Publisher protocol์— ์ •์˜๋œ sink๋‚˜ assign ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ๊ฐ์˜ subscription์„ ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์–ด์š”!

์ง€๊ธˆ๋„ ์ถฉ๋ถ„ํžˆ ๊ธ€์ด ๊ธด ๊ฒƒ ๊ฐ™์œผ๋‹ˆ..! ์ด ๋ฐฉ๋ฒ•๋“ค์€ ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ 20000!

์ด์ „ ํฌ์ŠคํŒ… ๐Ÿ‘ˆ๐Ÿป

๋‹ค์Œ ํฌ์ŠคํŒ… ๐Ÿ‘‰๐Ÿป

์ฐธ๊ณ 

--

--

Responses (1)