Skip to content

Latest commit

ย 

History

History
53 lines (30 loc) ยท 3.71 KB

MainThread.md

File metadata and controls

53 lines (30 loc) ยท 3.71 KB

UIKit ํด๋ž˜์Šค๋“ค์„ ๋‹ค๋ฃฐ ๋•Œ ๊ผญ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์“ฐ๋ ˆ๋“œ ์ด๋ฆ„์€ ๋ฌด์—‡์ธ๊ฐ€

๋„์›€์ด ๋œ ๊ธ€

๋„์›€์ด ๋  ๋‹ต๋ณ€

Answer

UI ์ฒ˜๋ฆฌ๋Š” Main Thread์—์„œ ์—…๋ฐ์ดํŠธ๊ฐ€ ์ด๋ฃจ์–ด์ ธ์•ผํ•œ๋‹ค.

main queue

  • ๋ฉ”์ธ ์Šค๋ ˆ๋“œ(UI ์Šค๋ ˆ๋“œ)์—์„œ ์‚ฌ์šฉ ๋˜๋Š” Serial Queue
  • ๋ชจ๋“  UI ์ฒ˜๋ฆฌ๋Š” ๋ฉ”์ธ ์Šค๋ ˆ๋“œ์—์„œ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•จ

UI์—…๋ฐ์ดํŠธ๊ฐ€ Background Thread์—์„œ ์ด๋ฃจ์–ด์ง€๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? ์ด๊ฒŒ Main Thread์˜ Blockํ˜„์ƒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ƒ๊ฐ ๋  ์ˆ˜ ์žˆ์ง€๋งŒ UI๋Š” Main Thread์—์„œ ์—…๋ฐ์ดํŠธ๊ฐ€ ์ด๋ฃจ์–ด์ ธ์•ผํ•œ๋‹ค.


๊ทธ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1. UIKit์€ Thread-Safeํ•˜์ง€ ์•Š๋‹ค.

atomic/nonatomic

UIKit์˜ ๋Œ€๋ถ€๋ถ„์˜ ์š”์†Œ๋“ค์€ Thread-Safeํ•˜์ง€ ์•Š๋‹ค(nonatomic). Apple์ด UIKit์„ ์ด๋ ‡๊ฒŒ ๋งŒ๋“  ์ด์œ ๋Š” ๊ฐ€์žฅ ๋‹จ์ˆœํ•˜๊ณ  ํฐ ์ด์œ ๋กœ UIKit๊ณผ ๊ฐ™์ด ๋งค์šฐ ํฐ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๋ชจ๋“  ์š”์†Œ๋“ค์ด Thread-Safeํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋„๋ก ๋งŒ๋“œ๋Š”๊ฒƒ์€ ๋งค์šฐ ๋ณต์žกํ•˜๊ณ  ์„ฑ๋Šฅ๋ฉด์—์„œ ํšจ์œจ์ ์ด์ง€ ์•Š๊ธฐ ๋–„๋ฌธ์ด๋‹ค. UI๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ์ง์ ‘์ ์œผ๋กœ ๋ณด์—ฌ์ง€๋Š” ๋ถ€๋ถ„์ด๋ฏ€๋กœ ์„ฑ๋Šฅ๊ณผ ํšจ์œจ, ์•ˆ์ •์„ฑ์€ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค.

Serial๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์œ„์™€๊ฐ™์€ ๋ฌธ์ œ๋Š” ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ. UI๋Š” Main Thread์—์„œ synchronouslyํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค.


2. iOS์˜ Graphics rendering(graphics pipeline)์€ ๊ถ๊ทน์ ์œผ๋กœ sync์ด๋‹ค.

์š”๊ธฐ์—์„œ๋„ ๊ณต๋ถ€ํ–ˆ๋“ฏ, ํ™”๋ฉดํ‘œ์‹œ(์‹ค์ œ๋กœ ๋ทฐ ์œ„์— ์ปจํ…์ธ ๋‚˜ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๊ทธ๋ฆฌ๋Š” ํ–‰์œ„)๋Š” UIKit์ž์‹ ์ด ์ง์ ‘ ํ–‰ํ•˜์ง€ ์•Š๊ณ  UIKit์— ์˜ํ•ด Core Animation์—๊ฒŒ ์œ„์ž„ํ•˜๊ฒŒ ๋œ๋‹ค.

์ด ๋–„, Core Animation์—์„œ๋Š” Core Animation Pipeline์„ ์‚ฌ์šฉํ•˜์—ฌ rendering์„ ํ•˜๋Š”๋ฐ

Core Animation Pipeline์˜ ์ฒ˜๋ฆฌ ๊ณผ์ •์€ ์ด 4๋‹จ๊ณ„๋กœ(Commit Transaction -> Render Server -> GPU -> Display)๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค.

๋Œ€์ถฉ ์ด๋Ÿฐ์‹์ธ๋ฐ..์ž์„ธํ•œ๊ฑด Why the UI need to be updated on Main Thread์„ ์ฐธ๊ณ ํ•˜์ž..

๋ฌดํŠผ ์ด๋Ÿฌํ•œ ์ „ ๊ณผ์ •์ด ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๋Š”๋ฐ์— ๋ถˆํŽธํ•จ์ด ์—†์œผ๋ ค๋ฉด ํ˜„ 60hz ์ฃผ์‚ฌ์œจ ๊ธฐ์ค€์œผ๋กœ 1/60์ดˆ์— ์ด๋ฃจ์–ด์ ธ์•ผํ•˜๋ฉฐ, ๊ณผ์ • ์ค‘์— application์ด ์ถฉ๋Œ์ด ๋‚˜๋ฉด ์•ˆ๋œ๋‹ค.

์ด๋ฅผ Main Thread์—์„œ ๋‹จ์ผํ™”ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ  ์—ฌ๋Ÿฌ Thread์—์„œ ๊ฐ๊ฐ ์—…๋ฐ์ดํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด, ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๋ทฐ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜์—ฌ ๋ Œ๋” ์„œ๋ฒ„๋กœ ์ „์†กํ•  ๊ฒƒ์ด๊ณ , ์ด์— ๋”ฐ๋ผ GPU ์— ๋งŽ์€ ๋ Œ๋”๋ง ์š”์ฒญ์„ ๋ณด๋‚ด๊ฒŒ ๋œ๋‹ค. ๋ Œ๋”๋ง์€ ์‹œ์Šคํ…œ ๋ฆฌ์†Œ์Šค๊ฐ€ ๋งŽ์ด ๋“œ๋Š” ์ž‘์—…์ด๋ฏ€๋กœ GPU ๊ฐ€ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์–ด ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๋งŒ์•ฝ(Main Thread๊ฐ€ ์•„๋‹Œ) BackGround Thread์—์„œ ๊ฐ์ž์˜ Run-Loop๋กœ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด, View๊ฐ€ ์ œ๋ฉ‹๋Œ€๋กœ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด, ๊ธฐ๊ธฐ๋ฅผ ํšŒ์ „ ํ–ˆ์„๋•Œ, ๋™์‹œ์— View์˜ Layout์ด ์žฌ๋ฐฐ์น˜๋˜์ง€ ์•Š๊ณ  ๊ฐ๊ฐ ๋”ฐ๋กœ๋”ฐ๋กœ ๋™์ž‘ํ•˜๋Š” ๋“ฑ)

์ด์— ๋ฐ˜ํ•ด, Main Thread์—์„œ๋Š” ๋ทฐ์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋ฐ›์•„์„œ ์ž์‹ ์˜ Run Loop์˜ ๋์—์„œ ํ•œ๋ฒˆ์— ๋ฐ˜์˜ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒํ•˜๋ฉด ์•ฑ์ด ๋ชจ๋“  ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ๊ฒƒ๋“ค์ด ๋™์‹œ์— ํ™œ์„ฑํ™” ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ Main Thread์˜ View Drawing Cycle์ด๋ผ๊ณ ํ•œ๋‹ค.